vuewrite 0.0.19 → 0.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/vuewrite.js +113 -38
- package/package.json +3 -2
package/dist/vuewrite.js
CHANGED
|
@@ -690,6 +690,112 @@ const _sfc_main$2 = defineComponent({
|
|
|
690
690
|
};
|
|
691
691
|
}
|
|
692
692
|
});
|
|
693
|
+
const createClipboardEvents = (store, props) => {
|
|
694
|
+
const getSelected = () => {
|
|
695
|
+
const [start, end, startIndex, endIndex] = store.startAndEnd;
|
|
696
|
+
if (startIndex === endIndex) {
|
|
697
|
+
const text2 = store.blocks[startIndex].text.slice(start.offset, end.offset);
|
|
698
|
+
return [
|
|
699
|
+
new ClipboardItem({
|
|
700
|
+
"text/plain": new Blob([text2], { type: "text/plain" })
|
|
701
|
+
})
|
|
702
|
+
];
|
|
703
|
+
}
|
|
704
|
+
const startText = store.blocks[startIndex].text.slice(start.offset);
|
|
705
|
+
const endText = store.blocks[endIndex].text.slice(0, end.offset);
|
|
706
|
+
const arr = [
|
|
707
|
+
{ type: store.blocks[startIndex].type, text: startText },
|
|
708
|
+
...store.blocks.slice(startIndex + 1, endIndex),
|
|
709
|
+
{ type: store.blocks[endIndex].type, text: endText }
|
|
710
|
+
];
|
|
711
|
+
const html = arr.map((item) => `<div>${item.text}</div>`).join("\n");
|
|
712
|
+
const text = arr.map((item) => item.text).join("\n");
|
|
713
|
+
return [
|
|
714
|
+
new ClipboardItem({
|
|
715
|
+
"text/html": new Blob([html], { type: "text/html" }),
|
|
716
|
+
"text/plain": new Blob([text], { type: "text/plain" })
|
|
717
|
+
})
|
|
718
|
+
];
|
|
719
|
+
};
|
|
720
|
+
const onCopy = (e) => {
|
|
721
|
+
if (e.defaultPrevented)
|
|
722
|
+
return;
|
|
723
|
+
e.preventDefault();
|
|
724
|
+
navigator.clipboard.write(getSelected());
|
|
725
|
+
store.history.push("setText");
|
|
726
|
+
};
|
|
727
|
+
const onCut = (e) => {
|
|
728
|
+
if (e.defaultPrevented)
|
|
729
|
+
return;
|
|
730
|
+
e.preventDefault();
|
|
731
|
+
navigator.clipboard.write(getSelected());
|
|
732
|
+
store.deleteSelected();
|
|
733
|
+
store.history.push("setText");
|
|
734
|
+
};
|
|
735
|
+
const insertText = (text) => {
|
|
736
|
+
if (props.preventMultiline) {
|
|
737
|
+
const blocks = text.split("\n");
|
|
738
|
+
store.insertText(blocks[0]);
|
|
739
|
+
for (let i = 1; i < blocks.length; i++) {
|
|
740
|
+
store.addNewLine();
|
|
741
|
+
store.insertText(blocks[i]);
|
|
742
|
+
}
|
|
743
|
+
} else {
|
|
744
|
+
store.insertText(text);
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
const parseHtml = (node) => {
|
|
748
|
+
var _a;
|
|
749
|
+
let isTextNode = false;
|
|
750
|
+
for (let _node of node.childNodes) {
|
|
751
|
+
if (_node.nodeType === Node.TEXT_NODE && ((_a = _node.textContent) == null ? void 0 : _a.trim()) || _node.nodeType == Node.ELEMENT_NODE && _node.tagName === "SPAN") {
|
|
752
|
+
isTextNode = true;
|
|
753
|
+
break;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
if (isTextNode) {
|
|
757
|
+
const text = node.textContent;
|
|
758
|
+
if (!text)
|
|
759
|
+
return;
|
|
760
|
+
insertText(text);
|
|
761
|
+
store.addNewLine();
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
for (let child of node.children) {
|
|
765
|
+
if (child.tagName === "DIV") {
|
|
766
|
+
parseHtml(child);
|
|
767
|
+
} else {
|
|
768
|
+
insertText(child.textContent ?? "");
|
|
769
|
+
store.addNewLine();
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
const parser = new DOMParser();
|
|
774
|
+
const onPaste = (e) => {
|
|
775
|
+
var _a, _b;
|
|
776
|
+
if (e.defaultPrevented)
|
|
777
|
+
return;
|
|
778
|
+
e.preventDefault();
|
|
779
|
+
const html = (_a = e.clipboardData) == null ? void 0 : _a.getData("text/html");
|
|
780
|
+
if (html) {
|
|
781
|
+
store.deleteSelected();
|
|
782
|
+
const dom = parser.parseFromString(html, "text/html");
|
|
783
|
+
parseHtml(dom.body);
|
|
784
|
+
} else {
|
|
785
|
+
const text = (_b = e.clipboardData) == null ? void 0 : _b.getData("text");
|
|
786
|
+
if (!text)
|
|
787
|
+
return;
|
|
788
|
+
store.deleteSelected();
|
|
789
|
+
insertText(text);
|
|
790
|
+
}
|
|
791
|
+
store.history.push("setText");
|
|
792
|
+
};
|
|
793
|
+
return {
|
|
794
|
+
onCopy,
|
|
795
|
+
onCut,
|
|
796
|
+
onPaste
|
|
797
|
+
};
|
|
798
|
+
};
|
|
693
799
|
const _sfc_main$1 = /* @__PURE__ */ defineComponent({
|
|
694
800
|
__name: "TextEditor",
|
|
695
801
|
props: {
|
|
@@ -856,41 +962,7 @@ const _sfc_main$1 = /* @__PURE__ */ defineComponent({
|
|
|
856
962
|
}
|
|
857
963
|
store.history.push("setText");
|
|
858
964
|
});
|
|
859
|
-
const onCopy = (
|
|
860
|
-
if (e.defaultPrevented)
|
|
861
|
-
return;
|
|
862
|
-
e.preventDefault();
|
|
863
|
-
navigator.clipboard.writeText(store.selectedText);
|
|
864
|
-
store.history.push("setText");
|
|
865
|
-
};
|
|
866
|
-
const onCut = (e) => {
|
|
867
|
-
if (e.defaultPrevented)
|
|
868
|
-
return;
|
|
869
|
-
e.preventDefault();
|
|
870
|
-
navigator.clipboard.writeText(store.selectedText);
|
|
871
|
-
store.deleteSelected();
|
|
872
|
-
store.history.push("setText");
|
|
873
|
-
};
|
|
874
|
-
const onPaste = (e) => {
|
|
875
|
-
var _a;
|
|
876
|
-
if (e.defaultPrevented)
|
|
877
|
-
return;
|
|
878
|
-
e.preventDefault();
|
|
879
|
-
const text = (_a = e.clipboardData) == null ? void 0 : _a.getData("Text");
|
|
880
|
-
if (!text)
|
|
881
|
-
return;
|
|
882
|
-
if (props.preventMultiline) {
|
|
883
|
-
const blocks = text.split("\n");
|
|
884
|
-
store.insertText(blocks[0]);
|
|
885
|
-
for (let i = 1; i < blocks.length; i++) {
|
|
886
|
-
store.addNewLine();
|
|
887
|
-
store.insertText(blocks[i]);
|
|
888
|
-
}
|
|
889
|
-
} else {
|
|
890
|
-
store.insertText(text);
|
|
891
|
-
}
|
|
892
|
-
store.history.push("setText");
|
|
893
|
-
};
|
|
965
|
+
const { onCut, onCopy, onPaste } = createClipboardEvents(store, props);
|
|
894
966
|
const getClientRects = (selection) => {
|
|
895
967
|
const anchor = getNode(selection.anchor.blockId);
|
|
896
968
|
const focus = getNode(selection.focus.blockId);
|
|
@@ -927,9 +999,12 @@ const _sfc_main$1 = /* @__PURE__ */ defineComponent({
|
|
|
927
999
|
onBeforeinput: _cache[0] || (_cache[0] = //@ts-ignore
|
|
928
1000
|
(...args) => unref(store).onInput && unref(store).onInput(...args)),
|
|
929
1001
|
onKeydown: onKeyDown,
|
|
930
|
-
onCopy
|
|
931
|
-
|
|
932
|
-
|
|
1002
|
+
onCopy: _cache[1] || (_cache[1] = //@ts-ignore
|
|
1003
|
+
(...args) => unref(onCopy) && unref(onCopy)(...args)),
|
|
1004
|
+
onPaste: _cache[2] || (_cache[2] = //@ts-ignore
|
|
1005
|
+
(...args) => unref(onPaste) && unref(onPaste)(...args)),
|
|
1006
|
+
onCut: _cache[3] || (_cache[3] = //@ts-ignore
|
|
1007
|
+
(...args) => unref(onCut) && unref(onCut)(...args))
|
|
933
1008
|
}, [
|
|
934
1009
|
(openBlock(true), createElementBlock(Fragment, null, renderList(unref(store).blocks, (block) => {
|
|
935
1010
|
return openBlock(), createBlock(_sfc_main$2, {
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "vuewrite",
|
|
3
3
|
"description": "Rich Text Editor based on Vue3 reactivity",
|
|
4
4
|
"private": false,
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.20",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "den59k",
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"preview": "vite preview"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"vue": "^3"
|
|
22
|
+
"vue": "^3",
|
|
23
|
+
"vuewrite": "^0.0.19"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"@vitejs/plugin-vue": "^5.0.4",
|