component.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 { reactive } from "vue";
  11. import { string } from "vue-types";
  12. import { useCompData } from ".";
  13. import { createUIComp } from "../../defines/createUIComp";
  14. export const Component = createUIComp({
  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],
  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. focus: false,
  56. });
  57. return () => (
  58. <div class={["w-1/1 h-1/1", state.focus && "drag-disable"]}>
  59. <ckeditor
  60. class={textStyle}
  61. editor={InlineEditor}
  62. onBlur={(e: any, editor: InlineEditor) => {
  63. state.focus = false;
  64. if (comp) {
  65. comp.value = editor.getData();
  66. } else {
  67. emit("update:value", editor.getData());
  68. }
  69. }}
  70. onFocus={() => (state.focus = true)}
  71. onReady={(editor: InlineEditor) => {
  72. editor.setData(comp?.value || props.value);
  73. if (!store.isEditMode) {
  74. editor.enableReadOnlyMode("editor");
  75. }
  76. }}
  77. config={config}
  78. />
  79. </div>
  80. );
  81. },
  82. });
  83. const textStyle = css`
  84. font-size: 0.14rem;
  85. p {
  86. margin: 0;
  87. }
  88. &.ck.ck-editor__editable_inline {
  89. > :last-child,
  90. > :first-child {
  91. margin-top: 0;
  92. margin-bottom: 0;
  93. }
  94. }
  95. `;