component.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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, 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 } = 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. watchEffect(() => {
  56. if (!store.textEditingState) {
  57. editorInstance?.setData(comp.value);
  58. }
  59. });
  60. return () => (
  61. <View
  62. class={[textStyle, store.currCompId === props.compId && "drag-disable"]}
  63. compId={props.compId}
  64. >
  65. <ckeditor
  66. class={textStyle}
  67. editor={InlineEditor}
  68. onBlur={() => {
  69. // actions.updateCompData(
  70. // helper.findComp(props.compId) as DesignComp,
  71. // "value",
  72. // editorInstance.getData()
  73. // );
  74. // store.setTextEditingState(false);
  75. }}
  76. onFocus={() => {
  77. store.setTextEditingState(true);
  78. }}
  79. onInput={(e: any) => {
  80. if (editorInstance) {
  81. actions.updateCompData(
  82. helper.findComp(props.compId) as DesignComp,
  83. "value",
  84. e
  85. );
  86. }
  87. }}
  88. onReady={(editor: InlineEditor) => {
  89. editorInstance = editor;
  90. editor.setData(comp.value);
  91. if (store.isPreview) {
  92. editor.enableReadOnlyMode("editor");
  93. }
  94. }}
  95. config={config}
  96. />
  97. </View>
  98. );
  99. },
  100. });
  101. const textStyle = css`
  102. font-size: 12px;
  103. color: #666;
  104. p {
  105. margin: 0;
  106. }
  107. .ck.ck-editor__editable_inline {
  108. cursor: text;
  109. overflow: hidden;
  110. pointer-events: none;
  111. > :last-child,
  112. > :first-child {
  113. margin-top: 0;
  114. margin-bottom: 0;
  115. }
  116. }
  117. &.drag-disable {
  118. .ck.ck-editor__editable_inline {
  119. pointer-events: auto;
  120. }
  121. }
  122. `;