@g9tm/lynxcontrols-code 0.0.0-stage → 2.10.0
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/README.md +124 -2
- package/catalog/cards/G9CodeEditor.md +17 -0
- package/catalog/cards/G9LogView.md +17 -0
- package/catalog/catalog.json +1462 -0
- package/catalog/index.md +6 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/editor/editor.css +229 -0
- package/dist/editor/editor.css.map +1 -0
- package/dist/editor/editor.d.ts +70 -0
- package/dist/editor/editor.d.ts.map +1 -0
- package/dist/editor/editor.jsx +678 -0
- package/dist/editor/index.d.ts +3 -0
- package/dist/editor/index.d.ts.map +1 -0
- package/dist/editor/index.jsx +2 -0
- package/dist/editor/model.d.ts +124 -0
- package/dist/editor/model.d.ts.map +1 -0
- package/dist/editor/model.js +437 -0
- package/dist/editor/model.js.map +1 -0
- package/dist/editor/model.jsx +435 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.jsx +2 -0
- package/dist/log/index.d.ts +3 -0
- package/dist/log/index.d.ts.map +1 -0
- package/dist/log/index.jsx +2 -0
- package/dist/log/log-view.css +167 -0
- package/dist/log/log-view.css.map +1 -0
- package/dist/log/log-view.d.ts +69 -0
- package/dist/log/log-view.d.ts.map +1 -0
- package/dist/log/log-view.jsx +285 -0
- package/dist/log/model.d.ts +91 -0
- package/dist/log/model.d.ts.map +1 -0
- package/dist/log/model.js +196 -0
- package/dist/log/model.js.map +1 -0
- package/dist/log/model.jsx +194 -0
- package/package.json +51 -4
|
@@ -0,0 +1,678 @@
|
|
|
1
|
+
import { invokeNode, measureNode } from "@g9tm/lynxcontrols/anchor";
|
|
2
|
+
import { g9SafeId, startTimer, stopTimer } from "@g9tm/lynxcontrols/base";
|
|
3
|
+
import { g9HeldModifiers, useG9Keys } from "@g9tm/lynxcontrols/commands";
|
|
4
|
+
import { g9KeyboardTargetProps } from "@g9tm/lynxcontrols/host";
|
|
5
|
+
import { useG9Theme } from "@g9tm/lynxcontrols/provider";
|
|
6
|
+
import { memo, useEffect, useMemo, useRef, useState } from "@lynx-js/react";
|
|
7
|
+
import { G9EditHistory, applyChange, caretAt, clampPos, comparePos, detectEol, findAll, isCaret, moveCaret, newlineText, nextMatchIndex, orderedRange, splitLines, textInRange, tokenizeJsonLine, tokenizePlainLine, wordAt } from "./model.jsx";
|
|
8
|
+
import "./editor.css";
|
|
9
|
+
const SENTINEL = '\u200b';
|
|
10
|
+
const CW_SAMPLE = '0123456789012345678901234567890123456789';
|
|
11
|
+
const LONG_LINE = 2000;
|
|
12
|
+
const CodeLine = memo(function(props) {
|
|
13
|
+
if (0 === props.text.length) return <text className="g9-code__line"> </text>;
|
|
14
|
+
const runs = [];
|
|
15
|
+
let at = 0;
|
|
16
|
+
const plain = (from, to)=>runs.push(<text key={`p${from}`} className="g9-code__run" style={{
|
|
17
|
+
color: props.colors.plain
|
|
18
|
+
}}>
|
|
19
|
+
{props.text.slice(from, to)}
|
|
20
|
+
</text>);
|
|
21
|
+
for (const t of props.tokens)if (!(t.start < at) && !(t.end <= t.start)) {
|
|
22
|
+
if (t.start > at) plain(at, t.start);
|
|
23
|
+
runs.push(<text key={t.start} className="g9-code__run" style={{
|
|
24
|
+
color: props.colors[t.kind]
|
|
25
|
+
}}>
|
|
26
|
+
{props.text.slice(t.start, t.end)}
|
|
27
|
+
</text>);
|
|
28
|
+
at = t.end;
|
|
29
|
+
}
|
|
30
|
+
if (at < props.text.length) plain(at, props.text.length);
|
|
31
|
+
return <text className="g9-code__line" text-maxline="1">
|
|
32
|
+
{runs}
|
|
33
|
+
</text>;
|
|
34
|
+
}, (a, b)=>a.text === b.text && a.tokens === b.tokens && a.colors === b.colors && a.marked === b.marked);
|
|
35
|
+
let seq = 0;
|
|
36
|
+
function G9CodeEditor(props) {
|
|
37
|
+
var _props_lineHeight, _props_tabSize, _ref, _props_value, _props_value1, _props_fontSize, _props_findLabels, _fl_count, _props_fontSize1, _props_accessibilityLabel, _fl_placeholder, _fl_previous, _fl_next, _fl_close;
|
|
38
|
+
const theme = useG9Theme();
|
|
39
|
+
const uid = useMemo(()=>{
|
|
40
|
+
var _props_id;
|
|
41
|
+
return g9SafeId(null != (_props_id = props.id) ? _props_id : `g9code${++seq}`);
|
|
42
|
+
}, [
|
|
43
|
+
props.id
|
|
44
|
+
]);
|
|
45
|
+
const inputId = `${uid}__input`;
|
|
46
|
+
const findId = `${uid}__find`;
|
|
47
|
+
const lh = null != (_props_lineHeight = props.lineHeight) ? _props_lineHeight : 20;
|
|
48
|
+
const tab = ' '.repeat(Math.max(1, null != (_props_tabSize = props.tabSize) ? _props_tabSize : 2));
|
|
49
|
+
const latest = useRef(props);
|
|
50
|
+
latest.current = props;
|
|
51
|
+
const readOnly = true === props.readOnly;
|
|
52
|
+
const initial = null != (_ref = null != (_props_value = props.value) ? _props_value : props.defaultValue) ? _ref : '';
|
|
53
|
+
const eol = useRef(detectEol(initial));
|
|
54
|
+
const [lines, setLinesState] = useState(()=>splitLines(initial));
|
|
55
|
+
const linesRef = useRef(lines);
|
|
56
|
+
const [sel, setSelState] = useState(caretAt({
|
|
57
|
+
line: 0,
|
|
58
|
+
col: 0
|
|
59
|
+
}));
|
|
60
|
+
const selRef = useRef(sel);
|
|
61
|
+
const goal = useRef(0);
|
|
62
|
+
const history = useRef(new G9EditHistory());
|
|
63
|
+
const emitted = useRef(null != (_props_value1 = props.value) ? _props_value1 : null);
|
|
64
|
+
const valueTimer = useRef(null);
|
|
65
|
+
const setLines = (next)=>{
|
|
66
|
+
linesRef.current = next;
|
|
67
|
+
setLinesState(next);
|
|
68
|
+
};
|
|
69
|
+
const setSel = (next, keepGoal = false)=>{
|
|
70
|
+
const s = {
|
|
71
|
+
anchor: clampPos(linesRef.current, next.anchor),
|
|
72
|
+
head: clampPos(linesRef.current, next.head)
|
|
73
|
+
};
|
|
74
|
+
selRef.current = s;
|
|
75
|
+
if (!keepGoal) goal.current = s.head.col;
|
|
76
|
+
setSelState(s);
|
|
77
|
+
reveal(s.head);
|
|
78
|
+
};
|
|
79
|
+
const joined = ()=>linesRef.current.join(eol.current);
|
|
80
|
+
const scheduleValue = ()=>{
|
|
81
|
+
var _latest_current_valueDelay;
|
|
82
|
+
if (!latest.current.onValueChange) return;
|
|
83
|
+
stopTimer(valueTimer.current);
|
|
84
|
+
valueTimer.current = startTimer(()=>{
|
|
85
|
+
var _latest_current_onValueChange, _latest_current;
|
|
86
|
+
const v = joined();
|
|
87
|
+
emitted.current = v;
|
|
88
|
+
null == (_latest_current_onValueChange = (_latest_current = latest.current).onValueChange) || _latest_current_onValueChange.call(_latest_current, v);
|
|
89
|
+
}, null != (_latest_current_valueDelay = latest.current.valueDelay) ? _latest_current_valueDelay : 150);
|
|
90
|
+
};
|
|
91
|
+
useEffect(()=>()=>stopTimer(valueTimer.current), []);
|
|
92
|
+
useEffect(()=>{
|
|
93
|
+
if (void 0 === props.value || props.value === emitted.current) return;
|
|
94
|
+
emitted.current = props.value;
|
|
95
|
+
eol.current = detectEol(props.value);
|
|
96
|
+
setLines(splitLines(props.value));
|
|
97
|
+
history.current.clear();
|
|
98
|
+
setSel(caretAt(clampPos(linesRef.current, selRef.current.head)));
|
|
99
|
+
}, [
|
|
100
|
+
props.value
|
|
101
|
+
]);
|
|
102
|
+
const replaceSelection = (text1, kind, caretInText)=>{
|
|
103
|
+
var _latest_current_onChange, _latest_current;
|
|
104
|
+
if (readOnly) return;
|
|
105
|
+
const before = selRef.current;
|
|
106
|
+
const { from, to } = orderedRange(before);
|
|
107
|
+
const r = applyChange(linesRef.current, {
|
|
108
|
+
from,
|
|
109
|
+
to,
|
|
110
|
+
inserted: text1
|
|
111
|
+
}, eol.current);
|
|
112
|
+
linesRef.current = r.lines;
|
|
113
|
+
splitLines(text1);
|
|
114
|
+
const end = r.inverse.to;
|
|
115
|
+
let caret = end;
|
|
116
|
+
if (void 0 !== caretInText) {
|
|
117
|
+
const head = splitLines(text1.slice(0, caretInText));
|
|
118
|
+
caret = 1 === head.length ? {
|
|
119
|
+
line: from.line,
|
|
120
|
+
col: from.col + head[0].length
|
|
121
|
+
} : {
|
|
122
|
+
line: from.line + head.length - 1,
|
|
123
|
+
col: head[head.length - 1].length
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const after = caretAt(caret);
|
|
127
|
+
history.current.record(r.change, r.inverse, before, after, kind, Date.now());
|
|
128
|
+
setLines(r.lines);
|
|
129
|
+
setSel(after);
|
|
130
|
+
null == (_latest_current_onChange = (_latest_current = latest.current).onChange) || _latest_current_onChange.call(_latest_current, r.change);
|
|
131
|
+
scheduleValue();
|
|
132
|
+
};
|
|
133
|
+
const deleteBackward = ()=>{
|
|
134
|
+
if (readOnly) return;
|
|
135
|
+
const s = selRef.current;
|
|
136
|
+
if (isCaret(s)) {
|
|
137
|
+
const from = moveCaret(linesRef.current, s.head, 'left');
|
|
138
|
+
if (0 === comparePos(from, s.head)) return;
|
|
139
|
+
selRef.current = {
|
|
140
|
+
anchor: from,
|
|
141
|
+
head: s.head
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
replaceSelection('', 'delete');
|
|
145
|
+
};
|
|
146
|
+
const deleteForward = ()=>{
|
|
147
|
+
if (readOnly) return;
|
|
148
|
+
const s = selRef.current;
|
|
149
|
+
if (isCaret(s)) {
|
|
150
|
+
const to = moveCaret(linesRef.current, s.head, 'right');
|
|
151
|
+
if (0 === comparePos(to, s.head)) return;
|
|
152
|
+
selRef.current = {
|
|
153
|
+
anchor: s.head,
|
|
154
|
+
head: to
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
replaceSelection('', 'delete');
|
|
158
|
+
};
|
|
159
|
+
const undo = ()=>{
|
|
160
|
+
const step = history.current.undo(linesRef.current, eol.current);
|
|
161
|
+
if (!step) return;
|
|
162
|
+
setLines(step.lines);
|
|
163
|
+
setSel(step.selection);
|
|
164
|
+
scheduleValue();
|
|
165
|
+
};
|
|
166
|
+
const redo = ()=>{
|
|
167
|
+
const step = history.current.redo(linesRef.current, eol.current);
|
|
168
|
+
if (!step) return;
|
|
169
|
+
setLines(step.lines);
|
|
170
|
+
setSel(step.selection);
|
|
171
|
+
scheduleValue();
|
|
172
|
+
};
|
|
173
|
+
const selectedText = ()=>{
|
|
174
|
+
const { from, to } = orderedRange(selRef.current);
|
|
175
|
+
return textInRange(linesRef.current, from, to, eol.current);
|
|
176
|
+
};
|
|
177
|
+
const [box, setBox] = useState({
|
|
178
|
+
width: 0,
|
|
179
|
+
height: 0
|
|
180
|
+
});
|
|
181
|
+
const [cw, setCw] = useState(0);
|
|
182
|
+
const [scroll, setScrollState] = useState({
|
|
183
|
+
top: 0,
|
|
184
|
+
left: 0
|
|
185
|
+
});
|
|
186
|
+
const scrollRef = useRef(scroll);
|
|
187
|
+
const origin = useRef({
|
|
188
|
+
x: 0,
|
|
189
|
+
y: 0
|
|
190
|
+
});
|
|
191
|
+
const root = useRef(null);
|
|
192
|
+
const charW = cw > 0 ? cw : (null != (_props_fontSize = props.fontSize) ? _props_fontSize : 13) * 0.6;
|
|
193
|
+
const gutterW = false === props.lineNumbers ? 12 : Math.max(3, String(lines.length).length) * charW + 24;
|
|
194
|
+
const longest = useMemo(()=>{
|
|
195
|
+
let m = 0;
|
|
196
|
+
for (const l of lines)if (l.length > m) m = l.length;
|
|
197
|
+
return m;
|
|
198
|
+
}, [
|
|
199
|
+
lines
|
|
200
|
+
]);
|
|
201
|
+
const setScroll = (top, left)=>{
|
|
202
|
+
const maxTop = Math.max(0, linesRef.current.length * lh - box.height + lh);
|
|
203
|
+
const maxLeft = Math.max(0, longest * charW + 24 - (box.width - gutterW));
|
|
204
|
+
const next = {
|
|
205
|
+
top: Math.max(0, Math.min(maxTop, top)),
|
|
206
|
+
left: Math.max(0, Math.min(maxLeft, left))
|
|
207
|
+
};
|
|
208
|
+
if (next.top === scrollRef.current.top && next.left === scrollRef.current.left) return;
|
|
209
|
+
scrollRef.current = next;
|
|
210
|
+
setScrollState(next);
|
|
211
|
+
};
|
|
212
|
+
const reveal = (pos)=>{
|
|
213
|
+
const s = scrollRef.current;
|
|
214
|
+
const y = pos.line * lh;
|
|
215
|
+
const x = pos.col * charW;
|
|
216
|
+
const w = box.width - gutterW;
|
|
217
|
+
let top = s.top;
|
|
218
|
+
let left = s.left;
|
|
219
|
+
if (y < top) top = y;
|
|
220
|
+
else if (y + lh > top + box.height) top = y + lh - box.height;
|
|
221
|
+
if (x < left) left = Math.max(0, x - 24);
|
|
222
|
+
else if (x > left + w - 16) left = x - w + 48;
|
|
223
|
+
if (top !== s.top || left !== s.left) setScroll(top, left);
|
|
224
|
+
};
|
|
225
|
+
const rows = Math.max(1, Math.ceil(box.height / lh) + 1);
|
|
226
|
+
const first = Math.max(0, Math.floor(scroll.top / lh));
|
|
227
|
+
const last = Math.min(lines.length, first + rows);
|
|
228
|
+
const tokenize = 'function' == typeof props.language ? props.language : 'plain' === props.language ? tokenizePlainLine : tokenizeJsonLine;
|
|
229
|
+
const cache = useRef(new Map());
|
|
230
|
+
const tokensOf = (text1)=>{
|
|
231
|
+
let t = cache.current.get(text1);
|
|
232
|
+
if (!t) {
|
|
233
|
+
t = tokenize(text1);
|
|
234
|
+
if (cache.current.size > 4000) cache.current.clear();
|
|
235
|
+
cache.current.set(text1, t);
|
|
236
|
+
}
|
|
237
|
+
return t;
|
|
238
|
+
};
|
|
239
|
+
useEffect(()=>cache.current.clear(), [
|
|
240
|
+
tokenize
|
|
241
|
+
]);
|
|
242
|
+
const p = theme.palette;
|
|
243
|
+
const colors = useMemo(()=>{
|
|
244
|
+
var _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7;
|
|
245
|
+
var _props_colors, _props_colors1, _props_colors2, _props_colors3, _props_colors4, _props_colors5, _props_colors6, _props_colors7;
|
|
246
|
+
return {
|
|
247
|
+
key: null != (_ref = null == (_props_colors = props.colors) ? void 0 : _props_colors.key) ? _ref : p.Primary,
|
|
248
|
+
string: null != (_ref1 = null == (_props_colors1 = props.colors) ? void 0 : _props_colors1.string) ? _ref1 : p.Success,
|
|
249
|
+
number: null != (_ref2 = null == (_props_colors2 = props.colors) ? void 0 : _props_colors2.number) ? _ref2 : p.Tertiary,
|
|
250
|
+
boolean: null != (_ref3 = null == (_props_colors3 = props.colors) ? void 0 : _props_colors3.boolean) ? _ref3 : p.Warning,
|
|
251
|
+
null: null != (_ref4 = null == (_props_colors4 = props.colors) ? void 0 : _props_colors4.null) ? _ref4 : p.Error,
|
|
252
|
+
punctuation: null != (_ref5 = null == (_props_colors5 = props.colors) ? void 0 : _props_colors5.punctuation) ? _ref5 : p.OnSurfaceVariant,
|
|
253
|
+
comment: null != (_ref6 = null == (_props_colors6 = props.colors) ? void 0 : _props_colors6.comment) ? _ref6 : p.Outline,
|
|
254
|
+
plain: null != (_ref7 = null == (_props_colors7 = props.colors) ? void 0 : _props_colors7.plain) ? _ref7 : p.OnSurface
|
|
255
|
+
};
|
|
256
|
+
}, [
|
|
257
|
+
p,
|
|
258
|
+
props.colors
|
|
259
|
+
]);
|
|
260
|
+
const markersByLine = useMemo(()=>{
|
|
261
|
+
var _props_markers;
|
|
262
|
+
const m = new Map();
|
|
263
|
+
for (const mk of null != (_props_markers = props.markers) ? _props_markers : []){
|
|
264
|
+
const prev = m.get(mk.line - 1);
|
|
265
|
+
const rank = (s)=>'warning' === s ? 1 : 'info' === s ? 0 : 2;
|
|
266
|
+
if (!prev || rank(mk.severity) > rank(prev.severity)) m.set(mk.line - 1, mk);
|
|
267
|
+
}
|
|
268
|
+
return m;
|
|
269
|
+
}, [
|
|
270
|
+
props.markers
|
|
271
|
+
]);
|
|
272
|
+
const [openMarker, setOpenMarker] = useState(null);
|
|
273
|
+
const [find, setFind] = useState({
|
|
274
|
+
open: false,
|
|
275
|
+
query: '',
|
|
276
|
+
index: -1
|
|
277
|
+
});
|
|
278
|
+
const matches = useMemo(()=>find.open ? findAll(lines, find.query) : [], [
|
|
279
|
+
find.open,
|
|
280
|
+
find.query,
|
|
281
|
+
lines
|
|
282
|
+
]);
|
|
283
|
+
const goMatch = (dir)=>{
|
|
284
|
+
if (0 === matches.length) return;
|
|
285
|
+
const i = find.index >= 0 && find.index < matches.length ? (find.index + dir + matches.length) % matches.length : nextMatchIndex(matches, selRef.current.head, dir);
|
|
286
|
+
const m = matches[i];
|
|
287
|
+
setFind((f)=>({
|
|
288
|
+
...f,
|
|
289
|
+
index: i
|
|
290
|
+
}));
|
|
291
|
+
setSel({
|
|
292
|
+
anchor: {
|
|
293
|
+
line: m.line,
|
|
294
|
+
col: m.col
|
|
295
|
+
},
|
|
296
|
+
head: {
|
|
297
|
+
line: m.line,
|
|
298
|
+
col: m.col + m.length
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
};
|
|
302
|
+
const openFind = (query)=>{
|
|
303
|
+
var _selectedText_split_;
|
|
304
|
+
const q = null != query ? query : isCaret(selRef.current) ? find.query : null != (_selectedText_split_ = selectedText().split(/\r?\n/)[0]) ? _selectedText_split_ : '';
|
|
305
|
+
setFind({
|
|
306
|
+
open: true,
|
|
307
|
+
query: q,
|
|
308
|
+
index: -1
|
|
309
|
+
});
|
|
310
|
+
};
|
|
311
|
+
const closeFind = ()=>{
|
|
312
|
+
setFind((f)=>({
|
|
313
|
+
...f,
|
|
314
|
+
open: false,
|
|
315
|
+
index: -1
|
|
316
|
+
}));
|
|
317
|
+
focusInput();
|
|
318
|
+
};
|
|
319
|
+
const input1 = useRef(null);
|
|
320
|
+
const [focused, setFocused] = useState(false);
|
|
321
|
+
const composing = useRef(false);
|
|
322
|
+
const resetInput = ()=>void invokeNode(input1.current, 'setValue', {
|
|
323
|
+
value: SENTINEL
|
|
324
|
+
});
|
|
325
|
+
const focusInput = ()=>{
|
|
326
|
+
if (readOnly) return;
|
|
327
|
+
invokeNode(input1.current, 'focus');
|
|
328
|
+
};
|
|
329
|
+
const onInput = (e)=>{
|
|
330
|
+
var _e_detail_value;
|
|
331
|
+
const v = null != (_e_detail_value = e.detail.value) ? _e_detail_value : '';
|
|
332
|
+
if (e.detail.isComposing) {
|
|
333
|
+
composing.current = true;
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
composing.current = false;
|
|
337
|
+
if (v === SENTINEL) return;
|
|
338
|
+
if ('' === v) {
|
|
339
|
+
deleteBackward();
|
|
340
|
+
resetInput();
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const text1 = v.startsWith(SENTINEL) ? v.slice(SENTINEL.length) : v.replace(SENTINEL, '');
|
|
344
|
+
if (text1) if ('\n' === text1 || '\r\n' === text1) {
|
|
345
|
+
const nl = newlineText(linesRef.current, orderedRange(selRef.current).from, tab);
|
|
346
|
+
replaceSelection(nl.text, 'newline', nl.caret);
|
|
347
|
+
} else replaceSelection(text1, 1 === text1.length && ' ' !== text1 ? 'type' : 'other');
|
|
348
|
+
resetInput();
|
|
349
|
+
};
|
|
350
|
+
const lastKey = useRef({
|
|
351
|
+
sig: '',
|
|
352
|
+
at: 0,
|
|
353
|
+
source: ''
|
|
354
|
+
});
|
|
355
|
+
const moveBy = (move, extend)=>{
|
|
356
|
+
const s = selRef.current;
|
|
357
|
+
const vertical = 'up' === move || 'down' === move || 'pageUp' === move || 'pageDown' === move;
|
|
358
|
+
let target;
|
|
359
|
+
if (extend || isCaret(s) || 'left' !== move && 'right' !== move) target = moveCaret(linesRef.current, s.head, move, goal.current, Math.max(1, rows - 2));
|
|
360
|
+
else {
|
|
361
|
+
const r = orderedRange(s);
|
|
362
|
+
target = 'left' === move ? r.from : r.to;
|
|
363
|
+
}
|
|
364
|
+
setSel({
|
|
365
|
+
anchor: extend ? s.anchor : target,
|
|
366
|
+
head: target
|
|
367
|
+
}, vertical);
|
|
368
|
+
};
|
|
369
|
+
const handleKey = (e, source)=>{
|
|
370
|
+
const sig = `${e.key}|${e.ctrl ? 1 : 0}${e.meta ? 1 : 0}${e.alt ? 1 : 0}${e.shift ? 1 : 0}`;
|
|
371
|
+
const now = Date.now();
|
|
372
|
+
const l = lastKey.current;
|
|
373
|
+
if (l.sig === sig && l.source !== source && now - l.at < 40) {
|
|
374
|
+
lastKey.current = {
|
|
375
|
+
sig: '',
|
|
376
|
+
at: 0,
|
|
377
|
+
source: ''
|
|
378
|
+
};
|
|
379
|
+
return true;
|
|
380
|
+
}
|
|
381
|
+
lastKey.current = {
|
|
382
|
+
sig,
|
|
383
|
+
at: now,
|
|
384
|
+
source
|
|
385
|
+
};
|
|
386
|
+
const mod = e.ctrl || e.meta;
|
|
387
|
+
const k = 1 === e.key.length ? e.key.toLowerCase() : e.key;
|
|
388
|
+
const word = mod;
|
|
389
|
+
if (mod && 'f' === k) return openFind(), true;
|
|
390
|
+
if ('F3' === k || mod && 'g' === k) return goMatch(e.shift ? -1 : 1), true;
|
|
391
|
+
if ('Escape' === k && find.open) return closeFind(), true;
|
|
392
|
+
if (mod && 'a' === k) {
|
|
393
|
+
const end = moveCaret(linesRef.current, {
|
|
394
|
+
line: 0,
|
|
395
|
+
col: 0
|
|
396
|
+
}, 'docEnd');
|
|
397
|
+
return setSel({
|
|
398
|
+
anchor: {
|
|
399
|
+
line: 0,
|
|
400
|
+
col: 0
|
|
401
|
+
},
|
|
402
|
+
head: end
|
|
403
|
+
}), true;
|
|
404
|
+
}
|
|
405
|
+
if (mod && ('c' === k || 'x' === k)) {
|
|
406
|
+
if (isCaret(selRef.current) || !latest.current.onCopy) return true;
|
|
407
|
+
latest.current.onCopy(selectedText());
|
|
408
|
+
if ('x' === k) replaceSelection('', 'other');
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
if (mod && 'z' === k) return e.shift ? redo() : undo(), true;
|
|
412
|
+
if (mod && 'y' === k) return redo(), true;
|
|
413
|
+
const moves = {
|
|
414
|
+
ArrowLeft: word ? 'wordLeft' : 'left',
|
|
415
|
+
ArrowRight: word ? 'wordRight' : 'right',
|
|
416
|
+
ArrowUp: 'up',
|
|
417
|
+
ArrowDown: 'down',
|
|
418
|
+
Home: mod ? 'docStart' : 'lineStart',
|
|
419
|
+
End: mod ? 'docEnd' : 'lineEnd',
|
|
420
|
+
PageUp: 'pageUp',
|
|
421
|
+
PageDown: 'pageDown'
|
|
422
|
+
};
|
|
423
|
+
const move = moves[e.key];
|
|
424
|
+
if (move) return moveBy(move, true === e.shift), true;
|
|
425
|
+
if (readOnly) return false;
|
|
426
|
+
if ('Delete' === e.key) return deleteForward(), true;
|
|
427
|
+
if ('Tab' === e.key && !mod) return replaceSelection(tab, 'type'), true;
|
|
428
|
+
return false;
|
|
429
|
+
};
|
|
430
|
+
const findKey = (e)=>{
|
|
431
|
+
const k = 1 === e.key.length ? e.key.toLowerCase() : e.key;
|
|
432
|
+
return 'Escape' === k || 'F3' === k || (e.ctrl || e.meta) && ('g' === k || 'f' === k);
|
|
433
|
+
};
|
|
434
|
+
useG9Keys((e)=>{
|
|
435
|
+
if (e.field && e.fieldId !== inputId) {
|
|
436
|
+
if (void 0 !== e.fieldId && e.fieldId !== findId) return false;
|
|
437
|
+
if (!findKey(e)) return false;
|
|
438
|
+
}
|
|
439
|
+
return handleKey(e, 'page');
|
|
440
|
+
}, {
|
|
441
|
+
enabled: focused || find.open,
|
|
442
|
+
field: true
|
|
443
|
+
});
|
|
444
|
+
const pointPos = (x, y)=>clampPos(linesRef.current, {
|
|
445
|
+
line: Math.floor((y - origin.current.y + scrollRef.current.top) / lh),
|
|
446
|
+
col: Math.round((x - origin.current.x - gutterW + scrollRef.current.left) / charW)
|
|
447
|
+
});
|
|
448
|
+
const pressing = useRef(false);
|
|
449
|
+
const lastPress = useRef({
|
|
450
|
+
at: 0,
|
|
451
|
+
line: -1,
|
|
452
|
+
col: -1
|
|
453
|
+
});
|
|
454
|
+
const pointOf = (e)=>{
|
|
455
|
+
var _ref, _ref1, _e_pageX, _ref2, _ref3, _e_pageY;
|
|
456
|
+
var _e_detail, _e_detail1;
|
|
457
|
+
return {
|
|
458
|
+
x: null != (_ref = null != (_ref1 = null != (_e_pageX = e.pageX) ? _e_pageX : e.x) ? _ref1 : null == (_e_detail = e.detail) ? void 0 : _e_detail.x) ? _ref : 0,
|
|
459
|
+
y: null != (_ref2 = null != (_ref3 = null != (_e_pageY = e.pageY) ? _e_pageY : e.y) ? _ref3 : null == (_e_detail1 = e.detail) ? void 0 : _e_detail1.y) ? _ref2 : 0
|
|
460
|
+
};
|
|
461
|
+
};
|
|
462
|
+
const press = (e)=>{
|
|
463
|
+
var _e_button;
|
|
464
|
+
if ((null != (_e_button = e.button) ? _e_button : 0) !== 0) return;
|
|
465
|
+
const pt = pointOf(e);
|
|
466
|
+
const pos = pointPos(pt.x, pt.y);
|
|
467
|
+
const now = Date.now();
|
|
468
|
+
const double = now - lastPress.current.at < 400 && lastPress.current.line === pos.line && lastPress.current.col === pos.col;
|
|
469
|
+
lastPress.current = {
|
|
470
|
+
at: now,
|
|
471
|
+
line: pos.line,
|
|
472
|
+
col: pos.col
|
|
473
|
+
};
|
|
474
|
+
pressing.current = true;
|
|
475
|
+
if (double) setSel(wordAt(linesRef.current, pos));
|
|
476
|
+
else g9HeldModifiers().shift ? setSel({
|
|
477
|
+
anchor: selRef.current.anchor,
|
|
478
|
+
head: pos
|
|
479
|
+
}) : setSel(caretAt(pos));
|
|
480
|
+
focusInput();
|
|
481
|
+
};
|
|
482
|
+
const drag = (e)=>{
|
|
483
|
+
if (!pressing.current) return;
|
|
484
|
+
const pt = pointOf(e);
|
|
485
|
+
setSel({
|
|
486
|
+
anchor: selRef.current.anchor,
|
|
487
|
+
head: pointPos(pt.x, pt.y)
|
|
488
|
+
});
|
|
489
|
+
};
|
|
490
|
+
const touchFrom = useRef(null);
|
|
491
|
+
const onLayout = (e)=>{
|
|
492
|
+
setBox({
|
|
493
|
+
width: e.detail.width,
|
|
494
|
+
height: e.detail.height
|
|
495
|
+
});
|
|
496
|
+
measureNode(root.current).then((r)=>{
|
|
497
|
+
if (r) origin.current = {
|
|
498
|
+
x: r.x,
|
|
499
|
+
y: r.y
|
|
500
|
+
};
|
|
501
|
+
});
|
|
502
|
+
};
|
|
503
|
+
if (props.handleRef) props.handleRef.current = {
|
|
504
|
+
getValue: joined,
|
|
505
|
+
getSelectedText: selectedText,
|
|
506
|
+
setSelection: (s)=>setSel(s),
|
|
507
|
+
revealLine: (line)=>setSel(caretAt({
|
|
508
|
+
line: line - 1,
|
|
509
|
+
col: 0
|
|
510
|
+
})),
|
|
511
|
+
focus: focusInput,
|
|
512
|
+
undo,
|
|
513
|
+
redo,
|
|
514
|
+
openFind
|
|
515
|
+
};
|
|
516
|
+
const visibleLines = [];
|
|
517
|
+
const numbers = [];
|
|
518
|
+
const winFrom = Math.max(0, 1000 * Math.floor(scroll.left / charW / 1000) - 1000);
|
|
519
|
+
const winTo = winFrom + Math.ceil(Math.max(0, box.width - gutterW) / charW) + 3000;
|
|
520
|
+
for(let i = first; i < last; i++){
|
|
521
|
+
var _mk_severity, _mk_severity1;
|
|
522
|
+
const full = lines[i];
|
|
523
|
+
const cut = full.length > LONG_LINE ? Math.min(winFrom, full.length) : 0;
|
|
524
|
+
const text1 = full.length > LONG_LINE ? full.slice(cut, winTo) : full;
|
|
525
|
+
const mk = markersByLine.get(i);
|
|
526
|
+
visibleLines.push(<view key={i} className={`g9-code__row${mk ? ` g9-code__row--${null != (_mk_severity = mk.severity) ? _mk_severity : 'error'}` : ''}`} style={{
|
|
527
|
+
height: `${lh}px`,
|
|
528
|
+
paddingLeft: `${cut * charW}px`
|
|
529
|
+
}}>
|
|
530
|
+
<CodeLine index={i} text={text1} tokens={tokensOf(text1)} colors={colors} marked={mk ? mk.message : null}/>
|
|
531
|
+
</view>);
|
|
532
|
+
if (false !== props.lineNumbers || mk) numbers.push(<view key={i} className="g9-code__number-row" style={{
|
|
533
|
+
height: `${lh}px`
|
|
534
|
+
}}>
|
|
535
|
+
{mk ? <view className={`g9-code__mark g9-code__mark--${null != (_mk_severity1 = mk.severity) ? _mk_severity1 : 'error'}`} id={`${uid}__mark_${i + 1}`} bindtap={()=>setOpenMarker((o)=>o === i ? null : i)} bindmouseenter={()=>setOpenMarker(i)} bindmouseleave={()=>setOpenMarker((o)=>o === i ? null : o)} accessibility-element={true} accessibility-label={mk.message}/> : null}
|
|
536
|
+
{false === props.lineNumbers ? null : <text className="g9-code__number">{String(i + 1)}</text>}
|
|
537
|
+
</view>);
|
|
538
|
+
}
|
|
539
|
+
const { from, to } = orderedRange(sel);
|
|
540
|
+
const selRects = [];
|
|
541
|
+
if (!isCaret(sel)) for(let i = Math.max(first, from.line); i <= Math.min(last - 1, to.line); i++){
|
|
542
|
+
const a = i === from.line ? from.col : 0;
|
|
543
|
+
const b = i === to.line ? to.col : lines[i].length + 1;
|
|
544
|
+
if (!(b <= a)) selRects.push(<view key={i} className="g9-code__selection" style={{
|
|
545
|
+
top: `${i * lh - scroll.top}px`,
|
|
546
|
+
left: `${a * charW - scroll.left}px`,
|
|
547
|
+
width: `${(b - a) * charW}px`,
|
|
548
|
+
height: `${lh}px`
|
|
549
|
+
}}/>);
|
|
550
|
+
}
|
|
551
|
+
const hits = find.open ? matches.filter((m)=>m.line >= first && m.line < last).map((m)=><view key={`${m.line}:${m.col}`} className={`g9-code__hit${matches[find.index] === m ? ' g9-code__hit--current' : ''}`} style={{
|
|
552
|
+
top: `${m.line * lh - scroll.top}px`,
|
|
553
|
+
left: `${m.col * charW - scroll.left}px`,
|
|
554
|
+
width: `${m.length * charW}px`,
|
|
555
|
+
height: `${lh}px`
|
|
556
|
+
}}/>) : null;
|
|
557
|
+
const caretVisible = focused && !readOnly && sel.head.line >= first && sel.head.line < last;
|
|
558
|
+
const fl = null != (_props_findLabels = props.findLabels) ? _props_findLabels : {};
|
|
559
|
+
const countText = 0 === matches.length ? fl.count ? '0' : '0' : (null != (_fl_count = fl.count) ? _fl_count : '{0} / {1}').replace('{0}', String(find.index >= 0 ? find.index + 1 : 0)).replace('{1}', String(matches.length));
|
|
560
|
+
const openMk = null !== openMarker ? markersByLine.get(openMarker) : void 0;
|
|
561
|
+
return <view id={uid} className={`g9-code-editor${readOnly ? ' g9-code--readonly' : ''}${props.className ? ` ${props.className}` : ''}`} style={{
|
|
562
|
+
direction: 'ltr',
|
|
563
|
+
fontSize: `${null != (_props_fontSize1 = props.fontSize) ? _props_fontSize1 : 13}px`
|
|
564
|
+
}} ref={(r)=>{
|
|
565
|
+
root.current = r;
|
|
566
|
+
}} bindlayoutchange={onLayout} accessibility-element={true} accessibility-label={null != (_props_accessibilityLabel = props.accessibilityLabel) ? _props_accessibilityLabel : ''} bindwheel={(e)=>{
|
|
567
|
+
var _e_deltaY, _e_deltaX;
|
|
568
|
+
return setScroll(scrollRef.current.top + (null != (_e_deltaY = e.deltaY) ? _e_deltaY : 0), scrollRef.current.left + (null != (_e_deltaX = e.deltaX) ? _e_deltaX : 0));
|
|
569
|
+
}} bindmousedown={press} bindmousemove={drag} bindmouseup={()=>{
|
|
570
|
+
pressing.current = false;
|
|
571
|
+
}} bindtouchstart={(e)=>{
|
|
572
|
+
var _e_touches;
|
|
573
|
+
const t = null == (_e_touches = e.touches) ? void 0 : _e_touches[0];
|
|
574
|
+
if (t) touchFrom.current = {
|
|
575
|
+
x: t.pageX,
|
|
576
|
+
y: t.pageY,
|
|
577
|
+
top: scrollRef.current.top,
|
|
578
|
+
left: scrollRef.current.left,
|
|
579
|
+
moved: false
|
|
580
|
+
};
|
|
581
|
+
}} bindtouchmove={(e)=>{
|
|
582
|
+
var _e_touches;
|
|
583
|
+
const t = null == (_e_touches = e.touches) ? void 0 : _e_touches[0];
|
|
584
|
+
const f = touchFrom.current;
|
|
585
|
+
if (!t || !f) return;
|
|
586
|
+
if (Math.abs(t.pageX - f.x) + Math.abs(t.pageY - f.y) > 6) f.moved = true;
|
|
587
|
+
if (f.moved) setScroll(f.top - (t.pageY - f.y), f.left - (t.pageX - f.x));
|
|
588
|
+
}} bindtouchend={(e)=>{
|
|
589
|
+
const f = touchFrom.current;
|
|
590
|
+
touchFrom.current = null;
|
|
591
|
+
if (f && !f.moved) {
|
|
592
|
+
setSel(caretAt(pointPos(f.x, f.y)));
|
|
593
|
+
focusInput();
|
|
594
|
+
}
|
|
595
|
+
}}>
|
|
596
|
+
<view className="g9-code__gutter" style={{
|
|
597
|
+
width: `${gutterW}px`
|
|
598
|
+
}}>
|
|
599
|
+
<view className="g9-code__nums" style={{
|
|
600
|
+
transform: `translateY(${first * lh - scroll.top}px)`
|
|
601
|
+
}}>
|
|
602
|
+
{numbers}
|
|
603
|
+
</view>
|
|
604
|
+
</view>
|
|
605
|
+
<view className="g9-code__area">
|
|
606
|
+
<view className="g9-code__linebox" style={{
|
|
607
|
+
transform: `translate(${-scroll.left}px, ${first * lh - scroll.top}px)`
|
|
608
|
+
}}>
|
|
609
|
+
{visibleLines}
|
|
610
|
+
</view>
|
|
611
|
+
{hits}
|
|
612
|
+
{selRects}
|
|
613
|
+
{caretVisible ? <view className="g9-code__caret" style={{
|
|
614
|
+
top: `${sel.head.line * lh - scroll.top}px`,
|
|
615
|
+
left: `${sel.head.col * charW - scroll.left}px`,
|
|
616
|
+
height: `${lh}px`
|
|
617
|
+
}}/> : null}
|
|
618
|
+
<textarea {...g9KeyboardTargetProps} data-g9keys="all" id={inputId} ref={(r)=>{
|
|
619
|
+
input1.current = r;
|
|
620
|
+
}} className="g9-code__input" style={{
|
|
621
|
+
top: `${Math.max(0, sel.head.line * lh - scroll.top)}px`,
|
|
622
|
+
left: `${Math.max(0, sel.head.col * charW - scroll.left)}px`
|
|
623
|
+
}} {...{
|
|
624
|
+
value: SENTINEL
|
|
625
|
+
}} disabled={readOnly} ios-auto-correct={false} ios-spell-check={false} android-fullscreen-mode={false} bindinput={onInput} bindkeydown={(e)=>{
|
|
626
|
+
if ('string' != typeof e.key) return;
|
|
627
|
+
handleKey({
|
|
628
|
+
key: e.key,
|
|
629
|
+
...e.shiftKey ? {
|
|
630
|
+
shift: true
|
|
631
|
+
} : {},
|
|
632
|
+
...e.ctrlKey ? {
|
|
633
|
+
ctrl: true
|
|
634
|
+
} : {},
|
|
635
|
+
...e.altKey ? {
|
|
636
|
+
alt: true
|
|
637
|
+
} : {},
|
|
638
|
+
...e.metaKey ? {
|
|
639
|
+
meta: true
|
|
640
|
+
} : {},
|
|
641
|
+
field: true,
|
|
642
|
+
fieldId: inputId
|
|
643
|
+
}, 'own');
|
|
644
|
+
}} bindfocus={()=>setFocused(true)} bindblur={()=>setFocused(false)}/>
|
|
645
|
+
{openMk ? <view className="g9-code__tip" style={{
|
|
646
|
+
top: `${(openMarker + 1) * lh - scroll.top}px`
|
|
647
|
+
}}>
|
|
648
|
+
<text className="g9-code__tip-text">{openMk.message}</text>
|
|
649
|
+
</view> : null}
|
|
650
|
+
{find.open ? <view className="g9-code__find">
|
|
651
|
+
<input id={findId} className="g9-code__find-input" {...{
|
|
652
|
+
value: find.query
|
|
653
|
+
}} placeholder={null != (_fl_placeholder = fl.placeholder) ? _fl_placeholder : ''} confirm-type="search" bindinput={(e)=>setFind({
|
|
654
|
+
open: true,
|
|
655
|
+
query: e.detail.value,
|
|
656
|
+
index: -1
|
|
657
|
+
})} bindconfirm={()=>goMatch(g9HeldModifiers().shift ? -1 : 1)}/>
|
|
658
|
+
<text className="g9-code__find-count">{countText}</text>
|
|
659
|
+
<view id={`${uid}__find_prev`} className="g9-code__find-button" bindtap={()=>goMatch(-1)} accessibility-element={true} accessibility-label={null != (_fl_previous = fl.previous) ? _fl_previous : 'Previous'}>
|
|
660
|
+
<text className="g9-code__find-glyph">↑</text>
|
|
661
|
+
</view>
|
|
662
|
+
<view id={`${uid}__find_next`} className="g9-code__find-button" bindtap={()=>goMatch(1)} accessibility-element={true} accessibility-label={null != (_fl_next = fl.next) ? _fl_next : 'Next'}>
|
|
663
|
+
<text className="g9-code__find-glyph">↓</text>
|
|
664
|
+
</view>
|
|
665
|
+
<view id={`${uid}__find_close`} className="g9-code__find-button" bindtap={closeFind} accessibility-element={true} accessibility-label={null != (_fl_close = fl.close) ? _fl_close : 'Close'}>
|
|
666
|
+
<text className="g9-code__find-glyph">×</text>
|
|
667
|
+
</view>
|
|
668
|
+
</view> : null}
|
|
669
|
+
</view>
|
|
670
|
+
<text className="g9-code__measure" bindlayoutchange={(e)=>{
|
|
671
|
+
const w = e.detail.width / CW_SAMPLE.length;
|
|
672
|
+
if (w > 0 && Math.abs(w - cw) > 0.01) setCw(w);
|
|
673
|
+
}}>
|
|
674
|
+
{CW_SAMPLE}
|
|
675
|
+
</text>
|
|
676
|
+
</view>;
|
|
677
|
+
}
|
|
678
|
+
export { G9CodeEditor };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/editor/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,YAAY,EACjB,YAAY,EACZ,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,YAAY,GAClB,MAAM,UAAU,CAAC;AAClB,cAAc,SAAS,CAAC"}
|