@kerebron/extension-codejar 0.4.27 → 0.4.29
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/CodeJar.d.ts +51 -0
- package/esm/CodeJar.d.ts.map +1 -0
- package/esm/CodeJar.js +549 -0
- package/esm/CodeJar.js.map +1 -0
- package/esm/Decorator.d.ts +11 -0
- package/esm/Decorator.d.ts.map +1 -0
- package/esm/Decorator.js +46 -0
- package/esm/Decorator.js.map +1 -0
- package/esm/ExtensionCodeJar.d.ts +15 -0
- package/esm/ExtensionCodeJar.d.ts.map +1 -0
- package/esm/ExtensionCodeJar.js +62 -0
- package/esm/ExtensionCodeJar.js.map +1 -0
- package/esm/NodeCodeJar.d.ts +18 -0
- package/esm/NodeCodeJar.d.ts.map +1 -0
- package/esm/NodeCodeJar.js +70 -0
- package/esm/NodeCodeJar.js.map +1 -0
- package/esm/TreeSitterHighlighter.d.ts +11 -0
- package/esm/TreeSitterHighlighter.d.ts.map +1 -0
- package/esm/TreeSitterHighlighter.js +63 -0
- package/esm/TreeSitterHighlighter.js.map +1 -0
- package/esm/_dnt.shims.d.ts +2 -0
- package/esm/_dnt.shims.d.ts.map +1 -0
- package/esm/_dnt.shims.js +58 -0
- package/esm/_dnt.shims.js.map +1 -0
- package/esm/codeJarBlockNodeView.d.ts +6 -0
- package/esm/codeJarBlockNodeView.d.ts.map +1 -0
- package/esm/codeJarBlockNodeView.js +254 -0
- package/esm/codeJarBlockNodeView.js.map +1 -0
- package/esm/codeJarLineNumbers.d.ts +13 -0
- package/esm/codeJarLineNumbers.d.ts.map +1 -0
- package/esm/codeJarLineNumbers.js +86 -0
- package/esm/codeJarLineNumbers.js.map +1 -0
- package/esm/mod.d.ts +2 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +2 -0
- package/esm/mod.js.map +1 -0
- package/esm/package.json +3 -0
- package/esm/utils.d.ts +13 -0
- package/esm/utils.d.ts.map +1 -0
- package/esm/utils.js +49 -0
- package/esm/utils.js.map +1 -0
- package/package.json +11 -8
- package/src/CodeJar.ts +636 -0
- package/src/Decorator.ts +67 -0
- package/src/ExtensionCodeJar.ts +83 -0
- package/src/NodeCodeJar.ts +107 -0
- package/src/TreeSitterHighlighter.ts +85 -0
- package/src/_dnt.shims.ts +60 -0
- package/src/codeJarBlockNodeView.ts +372 -0
- package/src/codeJarLineNumbers.ts +129 -0
- package/src/mod.ts +1 -0
- package/src/utils.ts +77 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// This class was copied from https://github.com/antonmedv/codejar/blob/3.7.0/linenumbers.ts
|
|
2
|
+
// and applied with further improvements and fixes
|
|
3
|
+
|
|
4
|
+
type Options = {
|
|
5
|
+
class: string;
|
|
6
|
+
wrapClass: string;
|
|
7
|
+
width: string;
|
|
8
|
+
backgroundColor: string;
|
|
9
|
+
color: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const lineNumberOptions: Options = {
|
|
13
|
+
class: 'codejar-linenumbers',
|
|
14
|
+
wrapClass: 'codejar-wrap',
|
|
15
|
+
width: '35px',
|
|
16
|
+
backgroundColor: 'rgba(128, 128, 128, 0.15)',
|
|
17
|
+
color: '',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function withLineNumbers(
|
|
21
|
+
highlight: (e: HTMLElement) => void,
|
|
22
|
+
options: Partial<Options> = {},
|
|
23
|
+
) {
|
|
24
|
+
let lineNumbers: HTMLElement;
|
|
25
|
+
return function (editor: HTMLElement) {
|
|
26
|
+
highlight(editor);
|
|
27
|
+
|
|
28
|
+
if (!lineNumbers) {
|
|
29
|
+
lineNumbers = initLineNumbers(editor, {
|
|
30
|
+
...lineNumberOptions,
|
|
31
|
+
...options,
|
|
32
|
+
});
|
|
33
|
+
editor.addEventListener(
|
|
34
|
+
'scroll',
|
|
35
|
+
() => lineNumbers.style.top = `-${editor.scrollTop}px`,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const code = editor.textContent || '';
|
|
40
|
+
const linesCount = code.replace(/\n$/g, '').split('\n').length;
|
|
41
|
+
|
|
42
|
+
let text = '';
|
|
43
|
+
for (let i = 0; i < linesCount; i++) {
|
|
44
|
+
text += `${i + 1}\n`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
lineNumbers.innerText = text;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function refreshNumbers(
|
|
52
|
+
lineNumbers: HTMLElement,
|
|
53
|
+
editor: HTMLElement,
|
|
54
|
+
) {
|
|
55
|
+
const code = editor.textContent || '';
|
|
56
|
+
const linesCount = code.replace(/\n$/g, '').split('\n').length;
|
|
57
|
+
|
|
58
|
+
let text = '';
|
|
59
|
+
for (let i = 0; i < linesCount; i++) {
|
|
60
|
+
text += `${i + 1}\n`;
|
|
61
|
+
}
|
|
62
|
+
lineNumbers.innerText = text;
|
|
63
|
+
|
|
64
|
+
// Hack editor styles
|
|
65
|
+
editor.style.paddingLeft = `calc(40px + 5px)`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function initLineNumbers(
|
|
69
|
+
editor: HTMLElement,
|
|
70
|
+
opts: Options,
|
|
71
|
+
): HTMLElement {
|
|
72
|
+
const css = getComputedStyle(editor);
|
|
73
|
+
|
|
74
|
+
const wrap = document.createElement('div');
|
|
75
|
+
wrap.className = opts.wrapClass;
|
|
76
|
+
wrap.style.position = 'relative';
|
|
77
|
+
|
|
78
|
+
const innerWrap = document.createElement('div');
|
|
79
|
+
innerWrap.className = 'codejar-linenumbers-inner-wrap';
|
|
80
|
+
innerWrap.style.background = css.background;
|
|
81
|
+
innerWrap.style.marginTop = css.borderTopWidth;
|
|
82
|
+
innerWrap.style.marginBottom = css.borderBottomWidth;
|
|
83
|
+
innerWrap.style.marginLeft = css.borderLeftWidth;
|
|
84
|
+
innerWrap.style.borderTopLeftRadius = css.borderTopLeftRadius;
|
|
85
|
+
innerWrap.style.borderBottomLeftRadius = css.borderBottomLeftRadius;
|
|
86
|
+
|
|
87
|
+
const gutter = document.createElement('div');
|
|
88
|
+
gutter.className = opts.class;
|
|
89
|
+
innerWrap.appendChild(gutter);
|
|
90
|
+
|
|
91
|
+
wrap.appendChild(innerWrap);
|
|
92
|
+
|
|
93
|
+
// Add own styles
|
|
94
|
+
gutter.style.width = opts.width;
|
|
95
|
+
gutter.style.overflow = 'hidden';
|
|
96
|
+
gutter.style.backgroundColor = opts.backgroundColor;
|
|
97
|
+
|
|
98
|
+
// Copy editor styles
|
|
99
|
+
gutter.style.fontFamily = css.fontFamily;
|
|
100
|
+
gutter.style.fontSize = css.fontSize;
|
|
101
|
+
gutter.style.lineHeight = css.lineHeight;
|
|
102
|
+
gutter.style.paddingTop = `calc(${css.paddingTop})`;
|
|
103
|
+
gutter.style.paddingLeft = css.paddingLeft;
|
|
104
|
+
gutter.style.borderTopLeftRadius = css.borderTopLeftRadius;
|
|
105
|
+
gutter.style.borderBottomLeftRadius = css.borderBottomLeftRadius;
|
|
106
|
+
|
|
107
|
+
// Add line numbers
|
|
108
|
+
const lineNumbers = document.createElement('div');
|
|
109
|
+
lineNumbers.setAttribute('class', 'codejar-linenumber');
|
|
110
|
+
lineNumbers.style.color = opts.color || css.color;
|
|
111
|
+
lineNumbers.style.setProperty('mix-blend-mode', 'unset');
|
|
112
|
+
gutter.appendChild(lineNumbers);
|
|
113
|
+
|
|
114
|
+
// Tweak editor styles
|
|
115
|
+
editor.style.paddingLeft =
|
|
116
|
+
`calc(${opts.width} + ${gutter.style.paddingLeft} + 5px)`;
|
|
117
|
+
editor.style.whiteSpace = 'pre';
|
|
118
|
+
|
|
119
|
+
// Swap editor with a wrap
|
|
120
|
+
editor.parentNode!.insertBefore(wrap, editor);
|
|
121
|
+
wrap.appendChild(editor);
|
|
122
|
+
|
|
123
|
+
editor.addEventListener(
|
|
124
|
+
'scroll',
|
|
125
|
+
() => lineNumbers.style.top = `-${editor.scrollTop}px`,
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
return lineNumbers;
|
|
129
|
+
}
|
package/src/mod.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ExtensionCodeJar.js';
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Node } from 'prosemirror-model';
|
|
2
|
+
import { EditorView } from 'prosemirror-view';
|
|
3
|
+
import { CodeJar } from './CodeJar.js';
|
|
4
|
+
import { TextSelection } from 'prosemirror-state';
|
|
5
|
+
import { off } from 'node:process';
|
|
6
|
+
|
|
7
|
+
export function computeChange(oldVal: string, newVal: string) {
|
|
8
|
+
if (oldVal === newVal) return null;
|
|
9
|
+
let start = 0;
|
|
10
|
+
let oldEnd = oldVal.length;
|
|
11
|
+
let newEnd = newVal.length;
|
|
12
|
+
while (
|
|
13
|
+
start < oldEnd &&
|
|
14
|
+
oldVal.charCodeAt(start) === newVal.charCodeAt(start)
|
|
15
|
+
) {
|
|
16
|
+
start += 1;
|
|
17
|
+
}
|
|
18
|
+
while (
|
|
19
|
+
oldEnd > start &&
|
|
20
|
+
newEnd > start &&
|
|
21
|
+
oldVal.charCodeAt(oldEnd - 1) === newVal.charCodeAt(newEnd - 1)
|
|
22
|
+
) {
|
|
23
|
+
oldEnd -= 1;
|
|
24
|
+
newEnd -= 1;
|
|
25
|
+
}
|
|
26
|
+
return { from: start, to: oldEnd, text: newVal.slice(start, newEnd) };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const valueChanged = (
|
|
30
|
+
textUpdate: string,
|
|
31
|
+
node: Node,
|
|
32
|
+
getPos: () => number | undefined,
|
|
33
|
+
view: EditorView,
|
|
34
|
+
) => {
|
|
35
|
+
const change = computeChange(node.textContent, textUpdate);
|
|
36
|
+
|
|
37
|
+
if (change) {
|
|
38
|
+
const pos = getPos();
|
|
39
|
+
if ('undefined' !== typeof pos) {
|
|
40
|
+
const start = pos + 1;
|
|
41
|
+
|
|
42
|
+
let pmTr = view.state.tr;
|
|
43
|
+
pmTr = pmTr.replaceWith(
|
|
44
|
+
start + change.from,
|
|
45
|
+
start + change.to,
|
|
46
|
+
change.text ? view.state.schema.text(change.text) : [],
|
|
47
|
+
);
|
|
48
|
+
view.dispatch(pmTr);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const forwardSelection = (
|
|
54
|
+
codejar: CodeJar,
|
|
55
|
+
pmView: EditorView,
|
|
56
|
+
getPos: () => number | undefined,
|
|
57
|
+
) => {
|
|
58
|
+
// if (!cmView.hasFocus) return;
|
|
59
|
+
const selection = asProseMirrorSelection(pmView.state.doc, codejar, getPos);
|
|
60
|
+
if (!selection.eq(pmView.state.selection)) {
|
|
61
|
+
pmView.dispatch(pmView.state.tr.setSelection(selection));
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const asProseMirrorSelection = (
|
|
66
|
+
pmDoc: Node,
|
|
67
|
+
codejar: CodeJar,
|
|
68
|
+
getPos: () => number | undefined,
|
|
69
|
+
) => {
|
|
70
|
+
const offset = (typeof getPos === 'function' ? getPos() || 0 : 0) + 1;
|
|
71
|
+
const pos = codejar.save();
|
|
72
|
+
if (pos.dir === '<-') {
|
|
73
|
+
return TextSelection.create(pmDoc, pos.start + offset, pos.end + offset);
|
|
74
|
+
} else {
|
|
75
|
+
return TextSelection.create(pmDoc, pos.end + offset, pos.start + offset);
|
|
76
|
+
}
|
|
77
|
+
};
|