component.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { useEditor } from "@/modules/editor";
  2. import { Alignment } from "@ckeditor/ckeditor5-alignment";
  3. import { Bold, Italic } from "@ckeditor/ckeditor5-basic-styles";
  4. import { InlineEditor } from "@ckeditor/ckeditor5-editor-inline";
  5. import { Essentials } from "@ckeditor/ckeditor5-essentials";
  6. import { FontColor, FontFamily, FontSize } from "@ckeditor/ckeditor5-font";
  7. import { Link } from "@ckeditor/ckeditor5-link";
  8. import { Paragraph } from "@ckeditor/ckeditor5-paragraph";
  9. import { css } from "@linaria/core";
  10. import { defineComponent, ref, watch, watchEffect } from "vue";
  11. import { string } from "vue-types";
  12. import { useCompData } from ".";
  13. import { View } from "../View";
  14. import { DesignComp } from "@/modules/editor/objects/DesignTemp/DesignComp";
  15. export const Component = defineComponent({
  16. props: {
  17. compId: string().def(""),
  18. },
  19. setup(props) {
  20. const comp = useCompData(props.compId);
  21. const { store, helper, actions, controls } = useEditor();
  22. const config = {
  23. language: "zh-cn",
  24. plugins: [
  25. Essentials,
  26. Bold,
  27. Italic,
  28. Link,
  29. Paragraph,
  30. FontColor,
  31. FontSize,
  32. FontFamily,
  33. Alignment,
  34. ],
  35. fontSize: {
  36. options: [12, 14, 16, 18, 20, 24, 28, 32, 38, 42, 46, 52, 60],
  37. },
  38. toolbar: {
  39. items: [
  40. // "undo",
  41. // "redo",
  42. // "|",
  43. "fontColor",
  44. "fontsize",
  45. "bold",
  46. "italic",
  47. "|",
  48. "alignment",
  49. // "|",
  50. // "link",
  51. ],
  52. },
  53. };
  54. let editorInstance: InlineEditor;
  55. let timeOut = ref();
  56. watchEffect(() => {
  57. console.log("1111", store.textEditingState);
  58. if (!store.textEditingState) {
  59. editorInstance?.setData(comp.value);
  60. }
  61. });
  62. const toolbarClickListener = () => {
  63. editorInstance.ui.view.toolbar.element?.addEventListener("click", () => {
  64. if (timeOut.value) {
  65. clearTimeout(timeOut.value);
  66. timeOut.value = null;
  67. }
  68. });
  69. };
  70. return () => (
  71. <View
  72. class={[textStyle, store.currCompId === props.compId && "drag-disable"]}
  73. compId={props.compId}
  74. >
  75. <ckeditor
  76. class={textStyle}
  77. editor={InlineEditor}
  78. onBlur={() => {
  79. // actions.updateCompData(
  80. // helper.findComp(props.compId) as DesignComp,
  81. // "value",
  82. // editorInstance.getData()
  83. // );
  84. // store.setTextEditingState(false);
  85. // console.log("blur", editorInstance);
  86. timeOut.value = setTimeout(() => {
  87. store.setTextEditingState(false);
  88. controls.historyCtrl.history.submit();
  89. }, 500);
  90. }}
  91. onFocus={() => {
  92. store.setTextEditingState(true);
  93. }}
  94. onInput={(e: any) => {
  95. if (editorInstance) {
  96. actions.setTextarea(
  97. helper.findComp(props.compId) as DesignComp,
  98. "value",
  99. e
  100. );
  101. }
  102. }}
  103. onReady={(editor: InlineEditor) => {
  104. editorInstance = editor;
  105. editor.setData(comp.value);
  106. toolbarClickListener();
  107. if (store.isPreview) {
  108. editor.enableReadOnlyMode("editor");
  109. }
  110. }}
  111. config={config}
  112. />
  113. </View>
  114. );
  115. },
  116. });
  117. const textStyle = css`
  118. font-size: 12px;
  119. color: #666;
  120. p {
  121. margin: 0;
  122. }
  123. .ck.ck-editor__editable_inline {
  124. cursor: text;
  125. overflow: hidden;
  126. pointer-events: none;
  127. > :last-child,
  128. > :first-child {
  129. margin-top: 0;
  130. margin-bottom: 0;
  131. }
  132. }
  133. &.drag-disable {
  134. .ck.ck-editor__editable_inline {
  135. pointer-events: auto;
  136. }
  137. }
  138. `;