@kerebron/extension-codejar 0.4.26 → 0.4.27

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/esm/utils.js DELETED
@@ -1,48 +0,0 @@
1
- import { TextSelection } from 'prosemirror-state';
2
- export function computeChange(oldVal, newVal) {
3
- if (oldVal === newVal)
4
- return null;
5
- let start = 0;
6
- let oldEnd = oldVal.length;
7
- let newEnd = newVal.length;
8
- while (start < oldEnd &&
9
- oldVal.charCodeAt(start) === newVal.charCodeAt(start)) {
10
- start += 1;
11
- }
12
- while (oldEnd > start &&
13
- newEnd > start &&
14
- oldVal.charCodeAt(oldEnd - 1) === newVal.charCodeAt(newEnd - 1)) {
15
- oldEnd -= 1;
16
- newEnd -= 1;
17
- }
18
- return { from: start, to: oldEnd, text: newVal.slice(start, newEnd) };
19
- }
20
- export const valueChanged = (textUpdate, node, getPos, view) => {
21
- const change = computeChange(node.textContent, textUpdate);
22
- if (change) {
23
- const pos = getPos();
24
- if ('undefined' !== typeof pos) {
25
- const start = pos + 1;
26
- let pmTr = view.state.tr;
27
- pmTr = pmTr.replaceWith(start + change.from, start + change.to, change.text ? view.state.schema.text(change.text) : []);
28
- view.dispatch(pmTr);
29
- }
30
- }
31
- };
32
- export const forwardSelection = (codejar, pmView, getPos) => {
33
- // if (!cmView.hasFocus) return;
34
- const selection = asProseMirrorSelection(pmView.state.doc, codejar, getPos);
35
- if (!selection.eq(pmView.state.selection)) {
36
- pmView.dispatch(pmView.state.tr.setSelection(selection));
37
- }
38
- };
39
- export const asProseMirrorSelection = (pmDoc, codejar, getPos) => {
40
- const offset = (typeof getPos === 'function' ? getPos() || 0 : 0) + 1;
41
- const pos = codejar.save();
42
- if (pos.dir === '<-') {
43
- return TextSelection.create(pmDoc, pos.start + offset, pos.end + offset);
44
- }
45
- else {
46
- return TextSelection.create(pmDoc, pos.end + offset, pos.start + offset);
47
- }
48
- };