component.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. plugins: [
  24. Essentials,
  25. Bold,
  26. Italic,
  27. Link,
  28. Paragraph,
  29. FontColor,
  30. FontSize,
  31. FontFamily,
  32. Alignment,
  33. ],
  34. fontSize: {
  35. options: [12, 14, 16, 18, 20, 24, 28, 32, 38, 42, 46, 52, 60],
  36. },
  37. toolbar: {
  38. items: [
  39. // "undo",
  40. // "redo",
  41. // "|",
  42. "fontColor",
  43. "fontsize",
  44. "bold",
  45. "italic",
  46. "|",
  47. "alignment",
  48. // "|",
  49. // "link",
  50. ],
  51. },
  52. };
  53. let editorInstance: InlineEditor;
  54. // watchEffect(() => {
  55. // if (!store.textEditingState) {
  56. // editorInstance?.setData(comp.value);
  57. // }
  58. // });
  59. return () => (
  60. <View
  61. class={[textStyle, store.currCompId === props.compId && "drag-disable"]}
  62. compId={props.compId}
  63. >
  64. <ckeditor
  65. class={textStyle}
  66. editor={InlineEditor}
  67. onBlur={() => {
  68. actions.updateCompData(
  69. helper.findComp(props.compId) as DesignComp,
  70. "value",
  71. editorInstance.getData()
  72. );
  73. store.setTextEditingState(false);
  74. }}
  75. onFocus={() => {
  76. store.setTextEditingState(true);
  77. }}
  78. onReady={(editor: InlineEditor) => {
  79. editorInstance = editor;
  80. editor.setData(comp.value);
  81. if (store.isPreview) {
  82. editor.enableReadOnlyMode("editor");
  83. }
  84. }}
  85. config={config}
  86. />
  87. </View>
  88. );
  89. },
  90. });
  91. const textStyle = css`
  92. font-size: 12px;
  93. color: #666;
  94. p {
  95. margin: 0;
  96. }
  97. .ck.ck-editor__editable_inline {
  98. cursor: text;
  99. overflow: hidden;
  100. pointer-events: none;
  101. > :last-child,
  102. > :first-child {
  103. margin-top: 0;
  104. margin-bottom: 0;
  105. }
  106. }
  107. &.drag-disable {
  108. .ck.ck-editor__editable_inline {
  109. pointer-events: auto;
  110. }
  111. }
  112. `;