component.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, reactive } from "vue";
  11. import { string } from "vue-types";
  12. import { useCompData } from ".";
  13. import { View } from "../View";
  14. export const Component = defineComponent({
  15. props: {
  16. compId: string(),
  17. value: string().def(""),
  18. },
  19. emits: ["update:value"],
  20. setup(props, { emit }) {
  21. const comp = props.compId ? useCompData(props.compId) : null;
  22. const { store } = useEditor();
  23. const config = {
  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. const state = reactive({
  55. editing: false,
  56. });
  57. let editorInstance: InlineEditor;
  58. return () => (
  59. <View
  60. class={[state.editing && "drag-disable"]}
  61. compId={props.compId}
  62. onDblclick={() => {
  63. if (store.isEditMode) {
  64. state.editing = true;
  65. editorInstance.disableReadOnlyMode("editor");
  66. editorInstance.focus();
  67. }
  68. }}
  69. >
  70. <ckeditor
  71. class={textStyle}
  72. editor={InlineEditor}
  73. onBlur={(e: any, editor: InlineEditor) => {
  74. state.editing = false;
  75. editor.enableReadOnlyMode("editor");
  76. if (comp) {
  77. comp.value = editor.getData();
  78. } else {
  79. emit("update:value", editor.getData());
  80. }
  81. }}
  82. onReady={(editor: InlineEditor) => {
  83. editorInstance = editor;
  84. editor.setData(comp?.value || props.value);
  85. editor.enableReadOnlyMode("editor");
  86. }}
  87. config={config}
  88. />
  89. </View>
  90. );
  91. },
  92. });
  93. const textStyle = css`
  94. font-size: 12px;
  95. p {
  96. margin: 0;
  97. }
  98. &.ck.ck-editor__editable_inline {
  99. > :last-child,
  100. > :first-child {
  101. margin-top: 0;
  102. margin-bottom: 0;
  103. }
  104. }
  105. `;