|
@@ -66,97 +66,126 @@ const EditorComp = defineComponent({
|
|
|
emits: ["lost"],
|
|
|
|
|
|
setup(props, { emit }) {
|
|
|
- const inputRef = ref();
|
|
|
let editorRefVal = ref<HeadlessEditor>().value;
|
|
|
const comp = useCompData(props.compId);
|
|
|
const { store, actions, helper, controls } = useEditor();
|
|
|
|
|
|
let blurCanceler: any = null;
|
|
|
- // onMounted(() => {
|
|
|
- // blurCanceler = blurHandle();
|
|
|
- // nextTick(() => {
|
|
|
- // initHeight();
|
|
|
- // });
|
|
|
- // });
|
|
|
+
|
|
|
// onUnmounted(() => {
|
|
|
// blurCanceler?.();
|
|
|
// });
|
|
|
- // const preHeight = ref<number>(0);
|
|
|
- // const initHeight = () => {
|
|
|
- // const h = helper.pxToDesignSize(inputRef.value?.$el.clientHeight);
|
|
|
- // const isChange = Math.abs(preHeight.value - h) > 1;
|
|
|
- // preHeight.value = h;
|
|
|
- // actions.updateCompData(comp, "layout.size.1", preHeight.value);
|
|
|
- // helper.extendStreamCard(store.currStreamCardId);
|
|
|
- // if (isChange) {
|
|
|
- // actions.selectObjs([]);
|
|
|
- // setTimeout(() => {
|
|
|
- // actions.selectObjs([props.compId]);
|
|
|
- // }, 0);
|
|
|
- // }
|
|
|
- // };
|
|
|
- // function isInCkBodyWrapper(dom: HTMLElement) {
|
|
|
- // if (editorInstance.value) {
|
|
|
- // const in1 =
|
|
|
- // editorInstance.value.ui.view.toolbar.element?.contains(dom) ||
|
|
|
- // editorInstance.value.ui.view.editable.element?.contains(dom);
|
|
|
- // if (in1) return true;
|
|
|
-
|
|
|
- // const ckBodyWrapper = document.querySelector(".ck-body-wrapper");
|
|
|
- // if (ckBodyWrapper === dom || ckBodyWrapper?.contains(dom)) {
|
|
|
- // return true;
|
|
|
- // }
|
|
|
- // }
|
|
|
- // return false;
|
|
|
- // }
|
|
|
-
|
|
|
- // function blurHandle() {
|
|
|
- // function blur(e: MouseEvent) {
|
|
|
- // const target = e.target as HTMLElement;
|
|
|
-
|
|
|
- // if (!editorInstance.value) return;
|
|
|
-
|
|
|
- // if (isInCkBodyWrapper(target)) {
|
|
|
- // e.stopPropagation();
|
|
|
- // return;
|
|
|
- // }
|
|
|
- // actions.submitUpdate();
|
|
|
- // emit("lost");
|
|
|
- // }
|
|
|
- // document.addEventListener("mousedown", blur, {
|
|
|
- // capture: true,
|
|
|
- // });
|
|
|
- // return () => {
|
|
|
- // document.removeEventListener("mousedown", blur, { capture: true });
|
|
|
- // };
|
|
|
- // }
|
|
|
+ const preHeight = ref<number>(0);
|
|
|
+ const initHeight = () => {
|
|
|
+ const dom: HTMLElement | null = document.querySelector(
|
|
|
+ `#editor_${props.compId}`
|
|
|
+ );
|
|
|
+ if (!dom) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ const h = helper.pxToDesignSize(dom.clientHeight);
|
|
|
+ const isChange = Math.abs(preHeight.value - h) > 1;
|
|
|
+ preHeight.value = h;
|
|
|
+ actions.updateCompData(comp, "layout.size.1", preHeight.value);
|
|
|
+ helper.extendStreamCard(store.currStreamCardId);
|
|
|
+ if (isChange) {
|
|
|
+ actions.selectObjs([]);
|
|
|
+ setTimeout(() => {
|
|
|
+ actions.selectObjs([props.compId]);
|
|
|
+ }, 0);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ function isInCkBodyWrapper(dom: HTMLElement) {
|
|
|
+ if (editorRefVal) {
|
|
|
+ const in1 =
|
|
|
+ editorRefVal.ui.view.toolbar.element?.contains(dom) ||
|
|
|
+ editorRefVal.ui.view.editable.element?.contains(dom);
|
|
|
+ if (in1) return true;
|
|
|
+
|
|
|
+ const toolbarWrapper = document.querySelector("#text_toolbar");
|
|
|
+ if (toolbarWrapper === dom || toolbarWrapper?.contains(dom)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ function blurHandle() {
|
|
|
+ function blur(e: MouseEvent) {
|
|
|
+ const target = e.target as HTMLElement;
|
|
|
+
|
|
|
+ if (!editorRefVal) return;
|
|
|
+
|
|
|
+ if (isInCkBodyWrapper(target)) {
|
|
|
+ e.stopPropagation();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ actions.submitUpdate();
|
|
|
+ // emit("lost");
|
|
|
+ }
|
|
|
+ document.addEventListener("mousedown", blur, {
|
|
|
+ capture: true,
|
|
|
+ });
|
|
|
+ return () => {
|
|
|
+ document.removeEventListener("mousedown", blur, { capture: true });
|
|
|
+ };
|
|
|
+ }
|
|
|
const initEditor = async () => {
|
|
|
const dom: HTMLElement | null = document.querySelector(
|
|
|
- `#${props.compId}`
|
|
|
+ `#editor_${props.compId}`
|
|
|
);
|
|
|
- if (dom) {
|
|
|
- editorRefVal = await HeadlessEditor.create(dom);
|
|
|
- editorRefVal.setData(comp.value);
|
|
|
- editorRefVal.focus();
|
|
|
- // const range = document.createRange();
|
|
|
- // range.selectNodeContents(inputRef.value.$el);
|
|
|
- // const selection = window.getSelection();
|
|
|
- // selection?.removeAllRanges();
|
|
|
- // selection?.addRange(range);
|
|
|
- editorRefVal.on("input", (value: any) => {
|
|
|
- if (comp.value !== value) {
|
|
|
- actions.updateCompData(comp, "value", value);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
+ if (!dom) {
|
|
|
+ return;
|
|
|
}
|
|
|
+ editorRefVal = await HeadlessEditor.create(dom);
|
|
|
+ editorRefVal.setData(comp.value);
|
|
|
+ editorRefVal.focus();
|
|
|
+ const range = document.createRange();
|
|
|
+ range.selectNodeContents(dom);
|
|
|
+ const selection = window.getSelection();
|
|
|
+ selection?.removeAllRanges();
|
|
|
+ selection?.addRange(range);
|
|
|
+
|
|
|
+ editorRefVal.model.document.on("change:data", () => {
|
|
|
+ const value = editorRefVal?.getData();
|
|
|
+ if (comp.value !== value) {
|
|
|
+ actions.updateCompData(comp, "value", value);
|
|
|
+ nextTick(() => {
|
|
|
+ const element = editorRefVal?.ui.view.editable.element || null;
|
|
|
+ if (!element) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const h = helper.pxToDesignSize(element.clientHeight);
|
|
|
+ const isChange = Math.abs(preHeight.value - h) > 1;
|
|
|
+ preHeight.value = h;
|
|
|
+ actions.updateCompDatas(
|
|
|
+ comp,
|
|
|
+ ["value", "layout.size.1"],
|
|
|
+ [value, preHeight.value]
|
|
|
+ );
|
|
|
+ helper.extendStreamCard(store.currStreamCardId);
|
|
|
+ if (isChange) {
|
|
|
+ console.log("changing=>", isChange);
|
|
|
+ actions.selectObjs([]);
|
|
|
+ setTimeout(() => {
|
|
|
+ actions.selectObjs([props.compId]);
|
|
|
+ }, 0);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ controls.textEditor = editorRefVal;
|
|
|
};
|
|
|
|
|
|
onMounted(() => {
|
|
|
initEditor();
|
|
|
+ // blurCanceler = blurHandle();
|
|
|
+ nextTick(() => {
|
|
|
+ initHeight();
|
|
|
+ });
|
|
|
});
|
|
|
return () => {
|
|
|
- return <div class={textStyle} id={props.compId} ref={inputRef}></div>;
|
|
|
+ return <div class={textStyle} id={`editor_${props.compId}`}></div>;
|
|
|
};
|
|
|
|
|
|
// return () => (
|