component.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { useEditor } from "@/modules/editor";
  2. import { Alignment } from "@ckeditor/ckeditor5-alignment";
  3. import {
  4. Bold,
  5. Italic,
  6. Strikethrough,
  7. Underline,
  8. } from "@ckeditor/ckeditor5-basic-styles";
  9. import { InlineEditor } from "@ckeditor/ckeditor5-editor-inline";
  10. import { Essentials } from "@ckeditor/ckeditor5-essentials";
  11. import { FontColor, FontFamily, FontSize } from "@ckeditor/ckeditor5-font";
  12. import { Link } from "@ckeditor/ckeditor5-link";
  13. import { Paragraph } from "@ckeditor/ckeditor5-paragraph";
  14. import { css } from "@linaria/core";
  15. import LineHeight from "ckeditor5-line-height-latest/src/lineheight";
  16. import { nextTick } from "process";
  17. import { defineComponent, onMounted, onUnmounted, reactive, ref } from "vue";
  18. import { string } from "vue-types";
  19. import { useCompData } from ".";
  20. import { View } from "../View";
  21. function GetConfig() {
  22. const fontSizeOptions = [];
  23. const list = [12, 14, 16, 18, 20, 24, 28, 32, 38, 42, 46, 52, 60];
  24. for (const s of list) {
  25. fontSizeOptions.push({ title: s + "", model: s + "px" });
  26. }
  27. const fontFamilyOptions = [
  28. { title: "默认字体", model: "" },
  29. { title: "宋体", model: "宋体,Songti,STSong,serif" },
  30. { title: "黑体", model: "黑体,Heiti,STHeiti,sans-serif" },
  31. { title: "仿宋", model: "仿宋,FangSong,STFangsong,serif" },
  32. { title: "楷体", model: "楷体,KaiTi,STKaiti,sans-serif" },
  33. ];
  34. const config = {
  35. // updateSourceElementOnDestroy: true,
  36. language: "zh-cn",
  37. plugins: [
  38. Essentials,
  39. Bold,
  40. Italic,
  41. Link,
  42. Underline,
  43. Strikethrough,
  44. Paragraph,
  45. FontColor,
  46. FontSize,
  47. FontFamily,
  48. Alignment,
  49. LineHeight,
  50. ],
  51. fontSize: {
  52. options: fontSizeOptions,
  53. supportAllValues: true,
  54. },
  55. fontFamily: {
  56. options: fontFamilyOptions,
  57. supportAllValues: true,
  58. },
  59. lineHeight: {
  60. options: [1, 1.5, 2, 2.5, 3],
  61. },
  62. toolbar: {
  63. items: [
  64. // "undo",
  65. // "redo",
  66. // "|",
  67. "fontColor",
  68. "fontFamily",
  69. "fontSize",
  70. "lineHeight",
  71. "bold",
  72. "italic",
  73. "underline",
  74. "strikethrough",
  75. "|",
  76. "alignment",
  77. // "|",
  78. "link",
  79. ],
  80. },
  81. };
  82. return config;
  83. }
  84. export const Component = defineComponent({
  85. props: {
  86. compId: string().def(""),
  87. },
  88. setup(props) {
  89. const comp = useCompData(props.compId);
  90. const { store, actions } = useEditor();
  91. const state = reactive({
  92. editableId: "",
  93. });
  94. if (store.isEditMode) {
  95. actions.on("textFocus", function (compId, focus) {
  96. if (compId != props.compId) return;
  97. if (focus) {
  98. state.editableId = "" + Date.now();
  99. return;
  100. }
  101. state.editableId = "";
  102. });
  103. }
  104. return () => (
  105. <View
  106. class={[textStyle]}
  107. compId={props.compId}
  108. onDblclick={() => {
  109. if (store.isEditMode) {
  110. state.editableId = "" + Date.now();
  111. }
  112. }}
  113. >
  114. {state.editableId ? (
  115. <EditorComp
  116. compId={props.compId}
  117. key={state.editableId}
  118. onLost={() => {
  119. state.editableId = "";
  120. }}
  121. />
  122. ) : (
  123. <div
  124. innerHTML={comp.value}
  125. class={[textStyle, store.isEditMode && `pointer-events-none`]}
  126. />
  127. )}
  128. </View>
  129. );
  130. },
  131. });
  132. const EditorComp = defineComponent({
  133. props: {
  134. compId: string().isRequired,
  135. },
  136. emits: ["lost"],
  137. setup(props, { emit }) {
  138. const inputRef = ref();
  139. let editorInstance = ref<InlineEditor>();
  140. const comp = useCompData(props.compId);
  141. const { store, actions, helper, controls } = useEditor();
  142. let blurCanceler: any = null;
  143. onMounted(() => {
  144. blurCanceler = blurHandle();
  145. nextTick(() => {
  146. initHeight();
  147. });
  148. });
  149. onUnmounted(() => {
  150. blurCanceler?.();
  151. });
  152. const preHeight = ref<number>(0);
  153. const initHeight = () => {
  154. const h = helper.pxToDesignSize(inputRef.value?.$el.clientHeight);
  155. const isChange = Math.abs(preHeight.value - h) > 1;
  156. preHeight.value = h;
  157. actions.updateCompData(comp, "layout.size.1", preHeight.value);
  158. helper.extendStreamCard(store.currStreamCardId);
  159. if (isChange) {
  160. actions.selectObjs([]);
  161. setTimeout(() => {
  162. actions.selectObjs([props.compId]);
  163. }, 0);
  164. }
  165. };
  166. function isInCkBodyWrapper(dom: HTMLElement) {
  167. if (editorInstance.value) {
  168. const in1 =
  169. editorInstance.value.ui.view.toolbar.element?.contains(dom) ||
  170. editorInstance.value.ui.view.editable.element?.contains(dom);
  171. if (in1) return true;
  172. const ckBodyWrapper = document.querySelector(".ck-body-wrapper");
  173. if (ckBodyWrapper === dom || ckBodyWrapper?.contains(dom)) {
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. function blurHandle() {
  180. function blur(e: MouseEvent) {
  181. const target = e.target as HTMLElement;
  182. if (!editorInstance.value) return;
  183. if (isInCkBodyWrapper(target)) {
  184. e.stopPropagation();
  185. return;
  186. }
  187. actions.submitUpdate();
  188. emit("lost");
  189. }
  190. document.addEventListener("mousedown", blur, {
  191. capture: true,
  192. });
  193. return () => {
  194. document.removeEventListener("mousedown", blur, { capture: true });
  195. };
  196. }
  197. return () => (
  198. <ckeditor
  199. class={textStyle}
  200. ref={inputRef}
  201. editor={InlineEditor}
  202. onInput={(value: any) => {
  203. if (editorInstance.value && comp.value !== value) {
  204. actions.updateCompData(comp, "value", value);
  205. nextTick(() => {
  206. const h = helper.pxToDesignSize(inputRef.value?.$el.clientHeight);
  207. const isChange = Math.abs(preHeight.value - h) > 1;
  208. preHeight.value = h;
  209. actions.updateCompDatas(
  210. comp,
  211. ["value", "layout.size.1"],
  212. [value, preHeight.value]
  213. );
  214. helper.extendStreamCard(store.currStreamCardId);
  215. if (isChange) {
  216. console.log("changing=>", isChange);
  217. actions.selectObjs([]);
  218. setTimeout(() => {
  219. actions.selectObjs([props.compId]);
  220. }, 0);
  221. }
  222. });
  223. }
  224. }}
  225. onReady={(editor: InlineEditor) => {
  226. editorInstance.value = editor;
  227. console.log("editor");
  228. editor.setData(comp.value);
  229. editor.focus();
  230. const range = document.createRange();
  231. range.selectNodeContents(inputRef.value.$el);
  232. const selection = window.getSelection();
  233. selection?.removeAllRanges();
  234. selection?.addRange(range);
  235. }}
  236. config={GetConfig()}
  237. />
  238. );
  239. },
  240. });
  241. const textStyle = css`
  242. font-size: 12px;
  243. width: 100%;
  244. color: #666;
  245. word-break: break-all;
  246. p {
  247. margin: 0;
  248. }
  249. .ck.ck-editor__editable_inline {
  250. cursor: text;
  251. overflow: hidden;
  252. border: none !important;
  253. > :last-child,
  254. > :first-child {
  255. margin-top: 0;
  256. margin-bottom: 0;
  257. }
  258. padding: 0 !important;
  259. }
  260. `;