@harborclient/sdk 1.0.55 → 1.0.57
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.
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import type { Extension } from '@codemirror/state';
|
|
2
|
+
import { EditorView } from '@codemirror/view';
|
|
2
3
|
import { type RenderHighlightedPlaceholderOptions } from './renderHighlightedPlaceholder.js';
|
|
3
4
|
/**
|
|
4
5
|
* Options for building a muted syntax-highlighted placeholder layer.
|
|
5
6
|
*/
|
|
6
7
|
export type SyntaxHighlightedPlaceholderOptions = RenderHighlightedPlaceholderOptions;
|
|
8
|
+
/**
|
|
9
|
+
* Returns whether the syntax-highlighted placeholder should be visible.
|
|
10
|
+
*
|
|
11
|
+
* @param view - Parent CodeMirror editor view.
|
|
12
|
+
* @param engaged - True after the user pressed inside the editor chrome.
|
|
13
|
+
*/
|
|
14
|
+
export declare function shouldShowSyntaxPlaceholder(view: EditorView, engaged: boolean): boolean;
|
|
7
15
|
/**
|
|
8
16
|
* Returns CodeMirror extensions that show muted syntax-highlighted placeholder content
|
|
9
17
|
* when the editor document is empty and unfocused.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"syntaxHighlightedPlaceholder.d.ts","sourceRoot":"","sources":["../../../src/components/CodeEditor/syntaxHighlightedPlaceholder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"syntaxHighlightedPlaceholder.d.ts","sourceRoot":"","sources":["../../../src/components/CodeEditor/syntaxHighlightedPlaceholder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAGL,UAAU,EAIX,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,KAAK,mCAAmC,EAEzC,MAAM,mCAAmC,CAAC;AAE3C;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAAG,mCAAmC,CAAC;AA6CtF;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAQvF;AAiGD;;;;;;GAMG;AACH,wBAAgB,kCAAkC,CAChD,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,mCAAmC,GAC3C,SAAS,EAAE,CA+Eb"}
|
|
@@ -28,13 +28,32 @@ class SyntaxHighlightedPlaceholderWidget extends WidgetType {
|
|
|
28
28
|
return true;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns whether focus currently lives inside the editor's DOM subtree.
|
|
33
|
+
*
|
|
34
|
+
* @param view - Parent CodeMirror editor view.
|
|
35
|
+
*/
|
|
36
|
+
function isEditorDomFocused(view) {
|
|
37
|
+
const active = view.root.activeElement;
|
|
38
|
+
if (active == null) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return view.dom.contains(active);
|
|
42
|
+
}
|
|
31
43
|
/**
|
|
32
44
|
* Returns whether the syntax-highlighted placeholder should be visible.
|
|
33
45
|
*
|
|
34
46
|
* @param view - Parent CodeMirror editor view.
|
|
47
|
+
* @param engaged - True after the user pressed inside the editor chrome.
|
|
35
48
|
*/
|
|
36
|
-
function shouldShowSyntaxPlaceholder(view) {
|
|
37
|
-
|
|
49
|
+
export function shouldShowSyntaxPlaceholder(view, engaged) {
|
|
50
|
+
if (view.state.doc.length > 0) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
if (engaged || isEditorDomFocused(view)) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
38
57
|
}
|
|
39
58
|
/**
|
|
40
59
|
* Builds widget decorations for the muted placeholder layer.
|
|
@@ -50,6 +69,75 @@ function buildSyntaxPlaceholderDecorations(text, options) {
|
|
|
50
69
|
}).range(0)
|
|
51
70
|
]);
|
|
52
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* ViewPlugin that tracks editor engagement and renders placeholder decorations.
|
|
74
|
+
*/
|
|
75
|
+
class SyntaxHighlightedPlaceholderPlugin {
|
|
76
|
+
text;
|
|
77
|
+
options;
|
|
78
|
+
placeholder;
|
|
79
|
+
/**
|
|
80
|
+
* True after a pointer press inside the editor; cleared when focus leaves the editor
|
|
81
|
+
* while the document is still empty.
|
|
82
|
+
*/
|
|
83
|
+
engaged = false;
|
|
84
|
+
/**
|
|
85
|
+
* @param view - Parent CodeMirror editor view.
|
|
86
|
+
* @param text - Placeholder source shown until the user engages or types.
|
|
87
|
+
* @param options - Highlighting and theme options.
|
|
88
|
+
*/
|
|
89
|
+
constructor(view, text, options) {
|
|
90
|
+
this.text = text;
|
|
91
|
+
this.options = options;
|
|
92
|
+
this.placeholder = this.compute(view);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Recomputes placeholder decorations from document length and engagement state.
|
|
96
|
+
*
|
|
97
|
+
* @param view - Parent CodeMirror editor view.
|
|
98
|
+
*/
|
|
99
|
+
compute(view) {
|
|
100
|
+
return shouldShowSyntaxPlaceholder(view, this.engaged)
|
|
101
|
+
? buildSyntaxPlaceholderDecorations(this.text, this.options)
|
|
102
|
+
: Decoration.none;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Applies a new engagement flag and refreshes decorations when it changes.
|
|
106
|
+
*
|
|
107
|
+
* @param view - Parent CodeMirror editor view.
|
|
108
|
+
* @param nextEngaged - Whether the user has pressed inside the editor chrome.
|
|
109
|
+
*/
|
|
110
|
+
setEngaged(view, nextEngaged) {
|
|
111
|
+
if (this.engaged === nextEngaged) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
this.engaged = nextEngaged;
|
|
115
|
+
this.placeholder = this.compute(view);
|
|
116
|
+
view.dispatch({});
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Shows or hides the placeholder when the document, focus, or engagement changes.
|
|
120
|
+
*
|
|
121
|
+
* @param update - Parent view update.
|
|
122
|
+
*/
|
|
123
|
+
update(update) {
|
|
124
|
+
if (update.docChanged || update.focusChanged || update.view.state.doc.length === 0) {
|
|
125
|
+
this.placeholder = this.compute(update.view);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
get decorations() {
|
|
129
|
+
return this.placeholder;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Returns whether a DOM event target lies inside the editor root element.
|
|
134
|
+
*
|
|
135
|
+
* @param view - Parent CodeMirror editor view.
|
|
136
|
+
* @param target - Event target node.
|
|
137
|
+
*/
|
|
138
|
+
function isEventTargetInEditor(view, target) {
|
|
139
|
+
return target instanceof Node && view.dom.contains(target);
|
|
140
|
+
}
|
|
53
141
|
/**
|
|
54
142
|
* Returns CodeMirror extensions that show muted syntax-highlighted placeholder content
|
|
55
143
|
* when the editor document is empty and unfocused.
|
|
@@ -58,37 +146,66 @@ function buildSyntaxPlaceholderDecorations(text, options) {
|
|
|
58
146
|
* @param options - Highlighting and theme options shared with the parent editor.
|
|
59
147
|
*/
|
|
60
148
|
export function createSyntaxHighlightedPlaceholder(text, options) {
|
|
61
|
-
const
|
|
62
|
-
placeholder;
|
|
149
|
+
const placeholderPlugin = ViewPlugin.fromClass(class extends SyntaxHighlightedPlaceholderPlugin {
|
|
63
150
|
/**
|
|
64
151
|
* Initializes placeholder decorations for an empty, unfocused document.
|
|
65
152
|
*
|
|
66
153
|
* @param view - Parent CodeMirror editor view.
|
|
67
154
|
*/
|
|
68
155
|
constructor(view) {
|
|
69
|
-
|
|
70
|
-
? buildSyntaxPlaceholderDecorations(text, options)
|
|
71
|
-
: Decoration.none;
|
|
156
|
+
super(view, text, options);
|
|
72
157
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
158
|
+
}, { decorations: (v) => v.decorations });
|
|
159
|
+
/**
|
|
160
|
+
* Marks the editor engaged on pointer presses inside the editor chrome only.
|
|
161
|
+
*
|
|
162
|
+
* @param view - Parent CodeMirror editor view.
|
|
163
|
+
* @param event - Pointer or mouse event from the editor root.
|
|
164
|
+
*/
|
|
165
|
+
const handleEditorPointerDown = (view, event) => {
|
|
166
|
+
if (view.state.doc.length > 0 || !isEventTargetInEditor(view, event.target)) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const plugin = view.plugin(placeholderPlugin);
|
|
170
|
+
if (plugin == null) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
plugin.setEngaged(view, true);
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Clears engagement after focus leaves the editor while the document is still empty.
|
|
177
|
+
*
|
|
178
|
+
* @param view - Parent CodeMirror editor view.
|
|
179
|
+
*/
|
|
180
|
+
const handleEditorFocusOut = (view) => {
|
|
181
|
+
requestAnimationFrame(() => {
|
|
182
|
+
if (view.state.doc.length > 0 || isEditorDomFocused(view)) {
|
|
80
183
|
return;
|
|
81
184
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
185
|
+
const plugin = view.plugin(placeholderPlugin);
|
|
186
|
+
if (plugin == null) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
plugin.setEngaged(view, false);
|
|
190
|
+
});
|
|
191
|
+
};
|
|
192
|
+
const engagementHandlers = EditorView.domEventHandlers({
|
|
193
|
+
mousedown(event, view) {
|
|
194
|
+
handleEditorPointerDown(view, event);
|
|
195
|
+
return false;
|
|
196
|
+
},
|
|
197
|
+
pointerdown(event, view) {
|
|
198
|
+
handleEditorPointerDown(view, event);
|
|
199
|
+
return false;
|
|
200
|
+
},
|
|
201
|
+
focusout(_event, view) {
|
|
202
|
+
handleEditorFocusOut(view);
|
|
203
|
+
return false;
|
|
88
204
|
}
|
|
89
|
-
}
|
|
205
|
+
});
|
|
90
206
|
return [
|
|
91
|
-
|
|
207
|
+
placeholderPlugin,
|
|
208
|
+
engagementHandlers,
|
|
92
209
|
EditorView.contentAttributes.of({ 'aria-placeholder': text }),
|
|
93
210
|
EditorView.theme({
|
|
94
211
|
'.cm-content:has(.cm-syntax-placeholder) .cm-activeLine': {
|