@kerebron/extension-codejar 0.4.28 → 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.
@@ -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
+ };
@@ -1,17 +0,0 @@
1
- .codejar-linenumbers-inner-wrap {
2
- position: absolute;
3
- top: 0px;
4
- left: 0px;
5
- bottom: 0px;
6
- overflow: hidden;
7
- }
8
-
9
- .codejar-linenumbers {
10
- mix-blend-mode: initial;
11
- height: 100%;
12
- }
13
-
14
- .codejar-linenumber {
15
- position: relative;
16
- top: 0px;
17
- }
@@ -1,135 +0,0 @@
1
- @import './codejar-linenumbers.css';
2
-
3
- .codejar-root {
4
- position: relative;
5
- }
6
-
7
- .codejar-root .codejar-select {
8
- right: 5px;
9
- top: 5px;
10
- position: absolute;
11
- opacity: 0;
12
- z-index: 99999;
13
- }
14
-
15
- .codejar-root:hover .codejar-select {
16
- opacity: 1;
17
- }
18
-
19
- .codejar {
20
- font-family: monospace;
21
- }
22
-
23
- /* TreeSitter */
24
-
25
- .ts-keyword {
26
- color: purple;
27
- font-weight: bold;
28
- }
29
- .ts-string {
30
- color: green;
31
- }
32
- .ts-comment {
33
- color: gray;
34
- font-style: italic;
35
- }
36
-
37
- /* Common captures */
38
- .ts-property {
39
- color: #905;
40
- } /* Property names (e.g., in objects/CSS) */
41
- .ts-function,
42
- .ts-function_call {
43
- color: #07a;
44
- } /* Functions */
45
- .ts-keyword {
46
- color: #d73;
47
- font-weight: bold;
48
- } /* Keywords like if, return */
49
- .ts-string {
50
- color: #690;
51
- } /* Strings */
52
- .ts-number {
53
- color: #099;
54
- } /* Numbers */
55
- .ts-comment {
56
- color: #998;
57
- font-style: italic;
58
- } /* Comments */
59
- .ts-perator {
60
- color: #9a6;
61
- } /* Operators like + - = */
62
- .ts-type {
63
- color: #458;
64
- font-weight: bold;
65
- } /* Types */
66
- .ts-variable {
67
- color: #333;
68
- } /* Variables */
69
- .ts-constant,
70
- .ts-constant_builtin {
71
- color: #0086b3;
72
- } /* Constants */
73
- .ts-punctuation,
74
- .ts-punctuation_bracket {
75
- color: #999;
76
- } /* Brackets, commas */
77
- .ts-namespace {
78
- color: #000;
79
- font-weight: bold;
80
- }
81
-
82
- /* More specific ones */
83
- .ts-function_builtin {
84
- color: #d73;
85
- }
86
- .ts-keyword_operator {
87
- color: #d73;
88
- }
89
- .ts-string_special {
90
- color: #d44;
91
- } /* e.g., regex */
92
- .ts-tag {
93
- color: #008080;
94
- } /* HTML/XML tags */
95
- .ts-attribute {
96
- color: #e90;
97
- } /* Attributes */
98
-
99
- @media (prefers-color-scheme: dark) {
100
- /* Dark mode adjustments */
101
- .ts-property {
102
- color: #9cdcfe;
103
- }
104
- .ts-function {
105
- color: #dcdcaa;
106
- }
107
- .ts-keyword {
108
- color: #c586c0;
109
- }
110
- .ts-string {
111
- color: #ce9178;
112
- }
113
- .ts-number {
114
- color: #b5cea8;
115
- }
116
- .ts-comment {
117
- color: #6a9955;
118
- }
119
- .ts-operator {
120
- color: #d4d4d4;
121
- }
122
- .ts-type {
123
- color: #4ec9b0;
124
- }
125
- .ts-constant {
126
- color: #4fc1ff;
127
- }
128
- }
129
-
130
- .ts-diff_plus {
131
- color: green;
132
- }
133
- .ts-diff_minus {
134
- color: red;
135
- }