vuewrite 0.0.3 → 0.0.5-a

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.
@@ -46,6 +46,7 @@ single?: boolean | undefined;
46
46
  modelValue?: string | string[] | undefined;
47
47
  styles?: Style[] | Style[][] | undefined;
48
48
  autofocus?: boolean | undefined;
49
+ autoselect?: boolean | undefined;
49
50
  }>, {
50
51
  currentStyles: ComputedRef<Map<string, Style>>;
51
52
  currentBlock: ComputedRef< {
@@ -86,6 +87,7 @@ single?: boolean | undefined;
86
87
  modelValue?: string | string[] | undefined;
87
88
  styles?: Style[] | Style[][] | undefined;
88
89
  autofocus?: boolean | undefined;
90
+ autoselect?: boolean | undefined;
89
91
  }>>> & {
90
92
  onKeydown?: ((...args: any[]) => any) | undefined;
91
93
  "onUpdate:modelValue"?: ((...args: any[]) => any) | undefined;
@@ -145,6 +147,8 @@ declare class TextEditorStore {
145
147
  meta?: any;
146
148
  }[];
147
149
  } | null;
150
+ _selectedText: ComputedRef<string>;
151
+ get selectedText(): string;
148
152
  moveOffset(newOffset: number): void;
149
153
  onInput(_e: Event): void;
150
154
  addNewLine(): void;
package/dist/vuewrite.js CHANGED
@@ -156,6 +156,18 @@ class TextEditorStore {
156
156
  return null;
157
157
  return this.blocks.find((item) => item.id === this.selection.anchor.blockId) ?? null;
158
158
  }));
159
+ __publicField(this, "_selectedText", computed(() => {
160
+ if (this.isCollapsed)
161
+ return "";
162
+ const [start, end, startIndex, endIndex] = this.startAndEnd;
163
+ if (startIndex === endIndex) {
164
+ return this.blocks[startIndex].text.slice(start.offset, end.offset);
165
+ }
166
+ const startText = this.blocks[startIndex].text.slice(start.offset);
167
+ const endText = this.blocks[endIndex].text.slice(0, end.offset);
168
+ const arr = [startText, ...this.blocks.slice(startIndex + 1, endIndex).map((block) => block.text), endText];
169
+ return arr.join("\n");
170
+ }));
159
171
  __publicField(this, "_currentStyles", computed(() => {
160
172
  const [start, end, startIndex, endIndex] = this.startAndEnd;
161
173
  const styles = /* @__PURE__ */ new Map();
@@ -179,6 +191,9 @@ class TextEditorStore {
179
191
  get currentBlock() {
180
192
  return this._currentBlock.value;
181
193
  }
194
+ get selectedText() {
195
+ return this._selectedText.value;
196
+ }
182
197
  moveOffset(newOffset) {
183
198
  const delta = newOffset - this.selection.anchor.offset;
184
199
  if (delta === 0)
@@ -400,7 +415,7 @@ const _sfc_main$1 = /* @__PURE__ */ defineComponent({
400
415
  return props.slots[props.block.type] ?? null;
401
416
  });
402
417
  const blockProps = {
403
- "data-block-id": props.block.id
418
+ "data-vw-block-id": props.block.id
404
419
  };
405
420
  const renderBlockPart = (text, styles) => {
406
421
  if (!props.decorator)
@@ -528,7 +543,8 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
528
543
  single: { type: Boolean },
529
544
  modelValue: {},
530
545
  styles: {},
531
- autofocus: { type: Boolean }
546
+ autofocus: { type: Boolean },
547
+ autoselect: { type: Boolean }
532
548
  },
533
549
  emits: ["keydown", "update:modelValue", "update:styles"],
534
550
  setup(__props, { expose: __expose, emit: __emit }) {
@@ -602,15 +618,15 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
602
618
  let cachedSelection = {};
603
619
  useEventListener(document, "selectionchange", () => {
604
620
  const sel = window.getSelection();
605
- const anchor = findParent(sel.anchorNode, (el) => el.hasAttribute("data-block-id"));
621
+ const anchor = findParent(sel.anchorNode, (el) => el.hasAttribute("data-vw-block-id"));
606
622
  if (anchor) {
607
623
  const offset = anchor === sel.anchorNode ? 0 : calcOffsetToNode(anchor, sel.anchorNode) + sel.anchorOffset;
608
- store.selection.anchor = { blockId: anchor.getAttribute("data-block-id"), offset };
624
+ store.selection.anchor = { blockId: anchor.getAttribute("data-vw-block-id"), offset };
609
625
  }
610
- const focus = findParent(sel.focusNode, (el) => el.hasAttribute("data-block-id"));
626
+ const focus = findParent(sel.focusNode, (el) => el.hasAttribute("data-vw-block-id"));
611
627
  if (focus) {
612
628
  const offset = anchor === sel.focusNode ? 0 : calcOffsetToNode(focus, sel.focusNode) + sel.focusOffset;
613
- store.selection.focus = { blockId: focus.getAttribute("data-block-id"), offset };
629
+ store.selection.focus = { blockId: focus.getAttribute("data-vw-block-id"), offset };
614
630
  }
615
631
  if (store.isFocused.value !== !!focus || !!anchor) {
616
632
  store.isFocused.value = !!focus || !!anchor;
@@ -640,10 +656,10 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
640
656
  let anchor = null;
641
657
  let focus = null;
642
658
  for (let item of textEditorRef.value.children) {
643
- if (item.getAttribute("data-block-id") === store.selection.anchor.blockId) {
659
+ if (item.getAttribute("data-vw-block-id") === store.selection.anchor.blockId) {
644
660
  anchor = item;
645
661
  }
646
- if (item.getAttribute("data-block-id") === store.selection.focus.blockId) {
662
+ if (item.getAttribute("data-vw-block-id") === store.selection.focus.blockId) {
647
663
  focus = item;
648
664
  }
649
665
  }
@@ -657,12 +673,33 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
657
673
  };
658
674
  watch(() => store.selection, applySelection, { deep: true, flush: "post" });
659
675
  onMounted(() => {
660
- var _a;
676
+ var _a, _b;
661
677
  if (props.autofocus) {
662
678
  (_a = textEditorRef.value) == null ? void 0 : _a.focus();
663
679
  applySelection();
664
680
  }
681
+ if (props.autoselect) {
682
+ (_b = textEditorRef.value) == null ? void 0 : _b.focus();
683
+ store.selectAll();
684
+ }
665
685
  });
686
+ const onCopy = (e) => {
687
+ e.preventDefault();
688
+ navigator.clipboard.writeText(store.selectedText);
689
+ };
690
+ const onCut = (e) => {
691
+ e.preventDefault();
692
+ navigator.clipboard.writeText(store.selectedText);
693
+ store.deleteSelected();
694
+ };
695
+ const onPaste = (e) => {
696
+ var _a;
697
+ e.preventDefault();
698
+ const text = (_a = e.clipboardData) == null ? void 0 : _a.getData("Text");
699
+ if (!text)
700
+ return;
701
+ store.insertText(text);
702
+ };
666
703
  __expose({
667
704
  currentStyles: store._currentStyles,
668
705
  currentBlock: store._currentBlock,
@@ -682,7 +719,10 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
682
719
  contenteditable: "",
683
720
  onInput: _cache[0] || (_cache[0] = //@ts-ignore
684
721
  (...args) => unref(store).onInput && unref(store).onInput(...args)),
685
- onKeydown: onKeyDown
722
+ onKeydown: onKeyDown,
723
+ onCopy,
724
+ onPaste,
725
+ onCut
686
726
  }, [
687
727
  (openBlock(true), createElementBlock(Fragment, null, renderList(unref(store).blocks, (block) => {
688
728
  return openBlock(), createBlock(_sfc_main$1, {
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.3",
5
+ "version": "0.0.5-a",
6
6
  "type": "module",
7
7
  "license": "MIT",
8
8
  "author": "den59k",