@daasboard/editor 0.0.0-stage → 0.1.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/LICENSE +5 -0
- package/README.md +6 -2
- package/dist/camera/CameraInput.d.ts +106 -0
- package/dist/camera/CameraInput.js +398 -0
- package/dist/clipboard/ObjectClipboard.d.ts +40 -0
- package/dist/clipboard/ObjectClipboard.js +243 -0
- package/dist/clipboard/PasteCommitError.d.ts +4 -0
- package/dist/clipboard/PasteCommitError.js +7 -0
- package/dist/clipboard/carrier.d.ts +5 -0
- package/dist/clipboard/carrier.js +59 -0
- package/dist/clipboard/objects.d.ts +21 -0
- package/dist/clipboard/objects.js +362 -0
- package/dist/clipboard/placement.d.ts +11 -0
- package/dist/clipboard/placement.js +21 -0
- package/dist/clipboard/systemImages.d.ts +1 -0
- package/dist/clipboard/systemImages.js +24 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +22 -0
- package/dist/input/TouchInput.d.ts +159 -0
- package/dist/input/TouchInput.js +656 -0
- package/dist/input/visiblePart.d.ts +8 -0
- package/dist/input/visiblePart.js +15 -0
- package/dist/interaction/InteractionLayer.d.ts +83 -0
- package/dist/interaction/InteractionLayer.js +520 -0
- package/dist/props/PropertyController.d.ts +130 -0
- package/dist/props/PropertyController.js +1448 -0
- package/dist/props/PropertyRegistry.d.ts +38 -0
- package/dist/props/PropertyRegistry.js +21 -0
- package/dist/props/builtinProperties.d.ts +23 -0
- package/dist/props/builtinProperties.js +350 -0
- package/dist/props/defaults.d.ts +6 -0
- package/dist/props/defaults.js +29 -0
- package/dist/selection/SelectionModel.d.ts +25 -0
- package/dist/selection/SelectionModel.js +79 -0
- package/dist/text/CanvasEditorView.d.ts +99 -0
- package/dist/text/CanvasEditorView.js +308 -0
- package/dist/text/InputSink.d.ts +157 -0
- package/dist/text/InputSink.js +841 -0
- package/dist/text/TextEditSession.d.ts +173 -0
- package/dist/text/TextEditSession.js +1328 -0
- package/dist/text/clipboard.d.ts +11 -0
- package/dist/text/clipboard.js +489 -0
- package/dist/text/commands.d.ts +48 -0
- package/dist/text/commands.js +329 -0
- package/dist/text/contextMirror.d.ts +51 -0
- package/dist/text/contextMirror.js +292 -0
- package/dist/text/flat.d.ts +35 -0
- package/dist/text/flat.js +556 -0
- package/dist/text/graphemes.d.ts +16 -0
- package/dist/text/graphemes.js +219 -0
- package/dist/text/index.d.ts +13 -0
- package/dist/text/index.js +13 -0
- package/dist/text/inputReducer.d.ts +24 -0
- package/dist/text/inputReducer.js +44 -0
- package/dist/text/keymap.d.ts +17 -0
- package/dist/text/keymap.js +148 -0
- package/dist/text/schema.d.ts +12 -0
- package/dist/text/schema.js +44 -0
- package/dist/text/sessionFocus.d.ts +20 -0
- package/dist/text/sessionFocus.js +51 -0
- package/dist/text/types.d.ts +114 -0
- package/dist/text/types.js +1 -0
- package/dist/tools/CreateTool.d.ts +78 -0
- package/dist/tools/CreateTool.js +258 -0
- package/dist/tools/SelectTool.d.ts +155 -0
- package/dist/tools/SelectTool.js +808 -0
- package/dist/tools/ToolManager.d.ts +63 -0
- package/dist/tools/ToolManager.js +146 -0
- package/dist/tools/connectorEndpoints.d.ts +17 -0
- package/dist/tools/connectorEndpoints.js +53 -0
- package/dist/tools/frameRotation.d.ts +8 -0
- package/dist/tools/frameRotation.js +66 -0
- package/dist/tools/gradientHandles.d.ts +67 -0
- package/dist/tools/gradientHandles.js +110 -0
- package/dist/tools/presetPaths.d.ts +5 -0
- package/dist/tools/presetPaths.js +49 -0
- package/dist/tools/snapResize.d.ts +7 -0
- package/dist/tools/snapResize.js +65 -0
- package/dist/tools/snapping.d.ts +30 -0
- package/dist/tools/snapping.js +280 -0
- package/dist/tools/transform.d.ts +52 -0
- package/dist/tools/transform.js +277 -0
- package/package.json +36 -3
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { LineMetrics, Rect, Renderer, TextLayoutHandle } from "@daasboard/render-core";
|
|
2
|
+
import type { EditorState, Transaction } from "prosemirror-state";
|
|
3
|
+
import type { HeadlessEditorView, LayoutProvider, TextblockDir } from "./types.js";
|
|
4
|
+
export declare const VERTICAL_MOTION = "canvasEditorView/verticalMotion";
|
|
5
|
+
export declare const CARET_AFFINITY = "canvasEditorView/caretAffinity";
|
|
6
|
+
export type CaretAffinity = "upstream" | "downstream";
|
|
7
|
+
export interface CaretHit {
|
|
8
|
+
offset: number;
|
|
9
|
+
affinity: CaretAffinity;
|
|
10
|
+
}
|
|
11
|
+
export interface LineInfo {
|
|
12
|
+
start: number;
|
|
13
|
+
end: number;
|
|
14
|
+
top: number;
|
|
15
|
+
height: number;
|
|
16
|
+
baseline: number;
|
|
17
|
+
left: number;
|
|
18
|
+
width: number;
|
|
19
|
+
paragraph: number;
|
|
20
|
+
first: boolean;
|
|
21
|
+
last: boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface LineGeometry {
|
|
24
|
+
goalX: number | null;
|
|
25
|
+
affinity: CaretAffinity;
|
|
26
|
+
lines(): LineInfo[] | null;
|
|
27
|
+
lineIndexAt(off: number): number;
|
|
28
|
+
caretLocal(pos: number): {
|
|
29
|
+
x: number;
|
|
30
|
+
y: number;
|
|
31
|
+
h: number;
|
|
32
|
+
line: number;
|
|
33
|
+
};
|
|
34
|
+
offsetOnLine(line: number, x: number, visual: boolean): number;
|
|
35
|
+
hitOnLine(line: number, x: number): CaretHit;
|
|
36
|
+
visualLineEnd(line: number): number;
|
|
37
|
+
}
|
|
38
|
+
export declare function lineGeometry(view: HeadlessEditorView | undefined): LineGeometry | null;
|
|
39
|
+
export interface CanvasEditorViewProps {
|
|
40
|
+
state: EditorState;
|
|
41
|
+
layout: LayoutProvider;
|
|
42
|
+
dispatchTransaction?: (tr: Transaction, view: CanvasEditorView) => void;
|
|
43
|
+
props?: Record<string, unknown>;
|
|
44
|
+
}
|
|
45
|
+
export declare function lineIndexIn(lines: readonly LineInfo[], off: number): number;
|
|
46
|
+
export declare function buildLines(metrics: readonly LineMetrics[], starts: readonly number[]): LineInfo[];
|
|
47
|
+
export declare function graphemeCaretEdge(renderer: Renderer, handle: TextLayoutHandle, start: number, end: number, trailing: boolean, line?: {
|
|
48
|
+
top: number;
|
|
49
|
+
height: number;
|
|
50
|
+
}): {
|
|
51
|
+
x: number;
|
|
52
|
+
y: number;
|
|
53
|
+
h: number;
|
|
54
|
+
} | null;
|
|
55
|
+
export declare class CanvasEditorView implements HeadlessEditorView, LineGeometry {
|
|
56
|
+
state: EditorState;
|
|
57
|
+
composing: boolean;
|
|
58
|
+
focused: boolean;
|
|
59
|
+
goalX: number | null;
|
|
60
|
+
affinity: CaretAffinity;
|
|
61
|
+
readonly provider: LayoutProvider;
|
|
62
|
+
private readonly dispatchTransaction;
|
|
63
|
+
private readonly props;
|
|
64
|
+
private dispatching;
|
|
65
|
+
private lineCache;
|
|
66
|
+
constructor(props: CanvasEditorViewProps);
|
|
67
|
+
dispatch(tr: Transaction): void;
|
|
68
|
+
updateState(state: EditorState): void;
|
|
69
|
+
hasFocus(): boolean;
|
|
70
|
+
focus(): void;
|
|
71
|
+
blur(): void;
|
|
72
|
+
someProp(name: string, f?: (value: unknown) => unknown): unknown;
|
|
73
|
+
endOfTextblock(dir: TextblockDir, state?: EditorState): boolean;
|
|
74
|
+
lines(): LineInfo[] | null;
|
|
75
|
+
private linesFor;
|
|
76
|
+
lineIndexAt(off: number): number;
|
|
77
|
+
private lineIndexWithAffinity;
|
|
78
|
+
private caretX;
|
|
79
|
+
caretLocal(pos: number, affinity?: CaretAffinity): {
|
|
80
|
+
x: number;
|
|
81
|
+
y: number;
|
|
82
|
+
h: number;
|
|
83
|
+
line: number;
|
|
84
|
+
};
|
|
85
|
+
coordsAtPos(pos: number): Rect;
|
|
86
|
+
hitOnLine(li: number, x: number): CaretHit;
|
|
87
|
+
offsetOnLine(li: number, x: number, _visual: boolean): number;
|
|
88
|
+
visualLineEnd(li: number): number;
|
|
89
|
+
posAtCoords(coords: {
|
|
90
|
+
left: number;
|
|
91
|
+
top: number;
|
|
92
|
+
}): {
|
|
93
|
+
pos: number;
|
|
94
|
+
inside: number;
|
|
95
|
+
affinity: CaretAffinity;
|
|
96
|
+
} | null;
|
|
97
|
+
}
|
|
98
|
+
export declare function caretRect(view: CanvasEditorView, pos: number): Rect;
|
|
99
|
+
export declare function selectionRects(view: CanvasEditorView, from: number, to: number): Rect[];
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { offsetToPmPos, paragraphIndexAtOffset, paragraphStarts, pmPosToOffset } from "./flat.js";
|
|
2
|
+
import { nextGraphemeEnd, prevGraphemeStart } from "./graphemes.js";
|
|
3
|
+
export const VERTICAL_MOTION = "canvasEditorView/verticalMotion";
|
|
4
|
+
export const CARET_AFFINITY = "canvasEditorView/caretAffinity";
|
|
5
|
+
export function lineGeometry(view) {
|
|
6
|
+
return view && typeof view.lines === "function" ? view : null;
|
|
7
|
+
}
|
|
8
|
+
const EMPTY_LINE_RECT_W = 4;
|
|
9
|
+
export function lineIndexIn(lines, off) {
|
|
10
|
+
let lo = 0;
|
|
11
|
+
let hi = lines.length - 1;
|
|
12
|
+
while (lo < hi) {
|
|
13
|
+
const mid = (lo + hi + 1) >> 1;
|
|
14
|
+
if (lines[mid].start <= off)
|
|
15
|
+
lo = mid;
|
|
16
|
+
else
|
|
17
|
+
hi = mid - 1;
|
|
18
|
+
}
|
|
19
|
+
return lo;
|
|
20
|
+
}
|
|
21
|
+
export function buildLines(metrics, starts) {
|
|
22
|
+
const n = starts.length - 1;
|
|
23
|
+
const groups = Array.from({ length: n }, () => []);
|
|
24
|
+
let para = 0;
|
|
25
|
+
for (const m of metrics) {
|
|
26
|
+
while (para < n - 1 && m.startIndex > starts[para + 1] - 1)
|
|
27
|
+
para++;
|
|
28
|
+
groups[para].push(m);
|
|
29
|
+
}
|
|
30
|
+
const lines = [];
|
|
31
|
+
let y = 0;
|
|
32
|
+
for (let i = 0; i < n; i++) {
|
|
33
|
+
const g = groups[i];
|
|
34
|
+
if (g.length === 0) {
|
|
35
|
+
lines.push({ start: starts[i], end: starts[i], top: y, height: 0, baseline: y, left: 0, width: 0, paragraph: i, first: true, last: true });
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
g.forEach((m, k) => {
|
|
39
|
+
lines.push({ start: m.startIndex, end: m.endIndex, top: y, height: m.height, baseline: m.baseline, left: m.left, width: m.width, paragraph: i, first: k === 0, last: k === g.length - 1 });
|
|
40
|
+
y += m.height;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return lines;
|
|
44
|
+
}
|
|
45
|
+
export function graphemeCaretEdge(renderer, handle, start, end, trailing, line) {
|
|
46
|
+
const rects = renderer.rectsForRange(handle, start, end).filter((r) => !line || (r.y < line.top + line.height && r.y + r.h > line.top));
|
|
47
|
+
if (!rects.length)
|
|
48
|
+
return null;
|
|
49
|
+
const first = rects[0];
|
|
50
|
+
const last = rects[rects.length - 1];
|
|
51
|
+
const y = line ? line.top + line.height / 2 : first.y + first.h / 2;
|
|
52
|
+
const left = renderer.glyphPositionAt(handle, first.x + first.w / 4, y);
|
|
53
|
+
const right = renderer.glyphPositionAt(handle, first.x + first.w * 3 / 4, y);
|
|
54
|
+
const rtl = left.offset > right.offset || (left.offset === right.offset && left.offset === end && left.affinity === "upstream");
|
|
55
|
+
return { x: trailing !== rtl ? last.x + last.w : first.x, y: last.y, h: last.h };
|
|
56
|
+
}
|
|
57
|
+
export class CanvasEditorView {
|
|
58
|
+
state;
|
|
59
|
+
composing = false;
|
|
60
|
+
focused = false;
|
|
61
|
+
goalX = null;
|
|
62
|
+
affinity = "downstream";
|
|
63
|
+
provider;
|
|
64
|
+
dispatchTransaction;
|
|
65
|
+
props;
|
|
66
|
+
dispatching = false;
|
|
67
|
+
lineCache = null;
|
|
68
|
+
constructor(props) {
|
|
69
|
+
this.state = props.state;
|
|
70
|
+
this.provider = props.layout;
|
|
71
|
+
this.dispatchTransaction = props.dispatchTransaction;
|
|
72
|
+
this.props = props.props ?? {};
|
|
73
|
+
this.dispatch = this.dispatch.bind(this);
|
|
74
|
+
}
|
|
75
|
+
dispatch(tr) {
|
|
76
|
+
const keepGoal = !!tr.getMeta(VERTICAL_MOTION);
|
|
77
|
+
const affinity = tr.getMeta(CARET_AFFINITY);
|
|
78
|
+
if (affinity)
|
|
79
|
+
this.affinity = affinity;
|
|
80
|
+
else if (tr.selectionSet || tr.docChanged)
|
|
81
|
+
this.affinity = "downstream";
|
|
82
|
+
this.dispatching = true;
|
|
83
|
+
try {
|
|
84
|
+
if (this.dispatchTransaction)
|
|
85
|
+
this.dispatchTransaction(tr, this);
|
|
86
|
+
else
|
|
87
|
+
this.updateState(this.state.apply(tr));
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
this.dispatching = false;
|
|
91
|
+
}
|
|
92
|
+
if (!keepGoal)
|
|
93
|
+
this.goalX = null;
|
|
94
|
+
}
|
|
95
|
+
updateState(state) {
|
|
96
|
+
if (!this.dispatching && (this.state.doc !== state.doc || !this.state.selection.eq(state.selection)))
|
|
97
|
+
this.affinity = "downstream";
|
|
98
|
+
this.state = state;
|
|
99
|
+
}
|
|
100
|
+
hasFocus() {
|
|
101
|
+
return this.focused;
|
|
102
|
+
}
|
|
103
|
+
focus() {
|
|
104
|
+
this.focused = true;
|
|
105
|
+
}
|
|
106
|
+
blur() {
|
|
107
|
+
this.focused = false;
|
|
108
|
+
}
|
|
109
|
+
someProp(name, f) {
|
|
110
|
+
const direct = this.props[name];
|
|
111
|
+
if (direct !== undefined) {
|
|
112
|
+
if (!f)
|
|
113
|
+
return direct;
|
|
114
|
+
const r = f(direct);
|
|
115
|
+
if (r)
|
|
116
|
+
return r;
|
|
117
|
+
}
|
|
118
|
+
for (const plugin of this.state.plugins) {
|
|
119
|
+
const v = plugin.props[name];
|
|
120
|
+
if (v === undefined)
|
|
121
|
+
continue;
|
|
122
|
+
if (!f)
|
|
123
|
+
return v;
|
|
124
|
+
const r = f(v);
|
|
125
|
+
if (r)
|
|
126
|
+
return r;
|
|
127
|
+
}
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
endOfTextblock(dir, state = this.state) {
|
|
131
|
+
const $head = state.selection.$head;
|
|
132
|
+
if (dir === "left" || dir === "backward")
|
|
133
|
+
return $head.parentOffset === 0;
|
|
134
|
+
if (dir === "right" || dir === "forward")
|
|
135
|
+
return $head.parentOffset === $head.parent.content.size;
|
|
136
|
+
const lines = this.linesFor(state.doc);
|
|
137
|
+
if (!lines || lines.length === 0)
|
|
138
|
+
return dir === "up" ? $head.parentOffset === 0 : $head.parentOffset === $head.parent.content.size;
|
|
139
|
+
const line = lines[this.lineIndexWithAffinity(lines, pmPosToOffset(state.doc, $head.pos), state === this.state ? this.affinity : "downstream")];
|
|
140
|
+
return dir === "up" ? line.first : line.last;
|
|
141
|
+
}
|
|
142
|
+
lines() {
|
|
143
|
+
return this.linesFor(this.state.doc);
|
|
144
|
+
}
|
|
145
|
+
linesFor(doc) {
|
|
146
|
+
const handle = this.provider.layout();
|
|
147
|
+
if (!handle)
|
|
148
|
+
return null;
|
|
149
|
+
const c = this.lineCache;
|
|
150
|
+
if (c && c.handle === handle && c.revision === handle.revision && c.doc === doc)
|
|
151
|
+
return c.lines;
|
|
152
|
+
const starts = paragraphStarts(doc);
|
|
153
|
+
const lines = buildLines(this.provider.renderer.lineMetrics(handle), starts);
|
|
154
|
+
for (let last = 1; last < lines.length; last++) {
|
|
155
|
+
const line = lines[last];
|
|
156
|
+
const lastChild = doc.child(line.paragraph).lastChild;
|
|
157
|
+
const endsWithBreak = lastChild?.type.name === "hard_break" || lastChild?.text?.endsWith("\u2028");
|
|
158
|
+
if (!line.last || !endsWithBreak || lines[last - 1].paragraph !== line.paragraph)
|
|
159
|
+
continue;
|
|
160
|
+
const end = starts[line.paragraph + 1] - 1;
|
|
161
|
+
lines[last - 1].end = end - 1;
|
|
162
|
+
line.start = end;
|
|
163
|
+
line.end = end;
|
|
164
|
+
}
|
|
165
|
+
this.lineCache = { handle, revision: handle.revision, doc, lines };
|
|
166
|
+
return lines;
|
|
167
|
+
}
|
|
168
|
+
lineIndexAt(off) {
|
|
169
|
+
const lines = this.lines();
|
|
170
|
+
return lines && lines.length ? this.lineIndexWithAffinity(lines, off, this.affinity) : 0;
|
|
171
|
+
}
|
|
172
|
+
lineIndexWithAffinity(lines, off, affinity) {
|
|
173
|
+
const li = lineIndexIn(lines, off);
|
|
174
|
+
return affinity === "upstream" && li > 0 && lines[li].start === off && lines[li - 1].end === off && lines[li - 1].paragraph === lines[li].paragraph ? li - 1 : li;
|
|
175
|
+
}
|
|
176
|
+
caretX(handle, line, off, affinity, softWrap) {
|
|
177
|
+
const text = this.state.doc.child(line.paragraph).textContent;
|
|
178
|
+
const paraStart = paragraphStarts(this.state.doc)[line.paragraph];
|
|
179
|
+
const local = off - paraStart;
|
|
180
|
+
const before = () => off > line.start ? graphemeCaretEdge(this.provider.renderer, handle, paraStart + prevGraphemeStart(text, local), off, true, line)?.x ?? null : null;
|
|
181
|
+
const after = () => off < line.end ? graphemeCaretEdge(this.provider.renderer, handle, off, paraStart + nextGraphemeEnd(text, local), false, line)?.x ?? null : null;
|
|
182
|
+
const x = (affinity === "upstream" ? before() ?? after() : after() ?? before()) ?? line.left;
|
|
183
|
+
return softWrap && off === line.end && affinity === "upstream" ? Math.max(line.left, x) : x;
|
|
184
|
+
}
|
|
185
|
+
caretLocal(pos, affinity = pos === this.state.selection.head ? this.affinity : "downstream") {
|
|
186
|
+
const handle = this.provider.layout();
|
|
187
|
+
const lines = this.lines();
|
|
188
|
+
if (!handle || !lines || lines.length === 0)
|
|
189
|
+
return { x: 0, y: 0, h: 0, line: -1 };
|
|
190
|
+
const off = pmPosToOffset(this.state.doc, pos);
|
|
191
|
+
const li = this.lineIndexWithAffinity(lines, off, affinity);
|
|
192
|
+
const line = lines[li];
|
|
193
|
+
return { x: this.caretX(handle, line, off, affinity, lines[li + 1]?.start === line.end), y: line.top, h: line.height, line: li };
|
|
194
|
+
}
|
|
195
|
+
coordsAtPos(pos) {
|
|
196
|
+
const o = this.provider.origin();
|
|
197
|
+
const c = this.caretLocal(pos);
|
|
198
|
+
return { x: o.x + c.x, y: o.y + c.y, w: 0, h: c.h };
|
|
199
|
+
}
|
|
200
|
+
hitOnLine(li, x) {
|
|
201
|
+
const handle = this.provider.layout();
|
|
202
|
+
const lines = this.lines();
|
|
203
|
+
if (!handle || !lines || lines.length === 0)
|
|
204
|
+
return { offset: 0, affinity: "downstream" };
|
|
205
|
+
const index = Math.max(0, Math.min(lines.length - 1, li));
|
|
206
|
+
const line = lines[index];
|
|
207
|
+
if (line.end === line.start)
|
|
208
|
+
return { offset: line.start, affinity: "downstream" };
|
|
209
|
+
const hit = this.provider.renderer.glyphPositionAt(handle, x, line.top + line.height / 2);
|
|
210
|
+
let offset = Math.max(line.start, Math.min(line.end, hit.offset));
|
|
211
|
+
if (lines[index + 1]?.start === line.end && x <= line.left && offset > line.start) {
|
|
212
|
+
const paraStart = paragraphStarts(this.state.doc)[line.paragraph];
|
|
213
|
+
const text = this.state.doc.child(line.paragraph).textContent;
|
|
214
|
+
if (/^[ \t]+$/u.test(text.slice(offset - paraStart, line.end - paraStart)))
|
|
215
|
+
offset = line.end;
|
|
216
|
+
}
|
|
217
|
+
return { offset, affinity: offset === line.end && lines[index + 1]?.start === offset ? "upstream" : hit.affinity };
|
|
218
|
+
}
|
|
219
|
+
offsetOnLine(li, x, _visual) {
|
|
220
|
+
return this.hitOnLine(li, x).offset;
|
|
221
|
+
}
|
|
222
|
+
visualLineEnd(li) {
|
|
223
|
+
const lines = this.lines();
|
|
224
|
+
if (!lines || lines.length === 0)
|
|
225
|
+
return 0;
|
|
226
|
+
const index = Math.max(0, Math.min(lines.length - 1, li));
|
|
227
|
+
const line = lines[index];
|
|
228
|
+
if (line.last || line.end <= line.start || lines[index + 1]?.start !== line.end)
|
|
229
|
+
return line.end;
|
|
230
|
+
const text = this.state.doc.child(line.paragraph).textContent;
|
|
231
|
+
const paraStart = paragraphStarts(this.state.doc)[line.paragraph];
|
|
232
|
+
const previous = prevGraphemeStart(text, line.end - paraStart);
|
|
233
|
+
return /^\s+$/u.test(text.slice(previous, line.end - paraStart)) ? paraStart + previous : line.end;
|
|
234
|
+
}
|
|
235
|
+
posAtCoords(coords) {
|
|
236
|
+
const handle = this.provider.layout();
|
|
237
|
+
const lines = this.lines();
|
|
238
|
+
if (!handle || !lines || lines.length === 0)
|
|
239
|
+
return null;
|
|
240
|
+
const o = this.provider.origin();
|
|
241
|
+
let x = coords.left - o.x;
|
|
242
|
+
let y = coords.top - o.y;
|
|
243
|
+
let boxLeft = 0;
|
|
244
|
+
let boxRight = handle.width;
|
|
245
|
+
for (const line of lines) {
|
|
246
|
+
boxLeft = Math.min(boxLeft, line.left);
|
|
247
|
+
boxRight = Math.max(boxRight, line.left + line.width);
|
|
248
|
+
}
|
|
249
|
+
const lastLine = lines[lines.length - 1];
|
|
250
|
+
const boxBottom = Math.max(handle.height, lastLine.top + lastLine.height);
|
|
251
|
+
const margin = lines[0].height;
|
|
252
|
+
if (x < boxLeft - margin || x > boxRight + margin || y < -margin || y > boxBottom + margin)
|
|
253
|
+
return null;
|
|
254
|
+
const inside = x >= boxLeft && x <= boxRight && y >= 0 && y <= boxBottom;
|
|
255
|
+
x = Math.max(boxLeft, Math.min(boxRight, x));
|
|
256
|
+
y = Math.max(0, Math.min(boxBottom, y));
|
|
257
|
+
let li = lines.length - 1;
|
|
258
|
+
for (let i = 0; i < lines.length; i++) {
|
|
259
|
+
if (y < lines[i].top + lines[i].height) {
|
|
260
|
+
li = i;
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const line = lines[li];
|
|
265
|
+
let hit = this.hitOnLine(li, x);
|
|
266
|
+
const doc = this.state.doc;
|
|
267
|
+
const text = doc.child(line.paragraph).textContent;
|
|
268
|
+
const paraStart = paragraphStarts(doc)[line.paragraph];
|
|
269
|
+
const tail = /[ \t]+$/u.exec(text.slice(line.start - paraStart, line.end - paraStart));
|
|
270
|
+
if (tail && coords.left - o.x > line.left + line.width && hit.offset >= line.start + tail.index)
|
|
271
|
+
hit = { offset: line.end, affinity: "upstream" };
|
|
272
|
+
const pi = line.paragraph;
|
|
273
|
+
return { pos: offsetToPmPos(doc, hit.offset), affinity: hit.affinity, inside: inside ? paragraphStarts(doc)[pi] + pi : -1 };
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
export function caretRect(view, pos) {
|
|
277
|
+
return view.coordsAtPos(pos);
|
|
278
|
+
}
|
|
279
|
+
export function selectionRects(view, from, to) {
|
|
280
|
+
const handle = view.provider.layout();
|
|
281
|
+
const lines = view.lines();
|
|
282
|
+
if (!handle || !lines || lines.length === 0)
|
|
283
|
+
return [];
|
|
284
|
+
const doc = view.state.doc;
|
|
285
|
+
const a = pmPosToOffset(doc, Math.min(from, to));
|
|
286
|
+
const b = pmPosToOffset(doc, Math.max(from, to));
|
|
287
|
+
if (b <= a)
|
|
288
|
+
return [];
|
|
289
|
+
const o = view.provider.origin();
|
|
290
|
+
const starts = paragraphStarts(doc);
|
|
291
|
+
const renderer = view.provider.renderer;
|
|
292
|
+
const out = [];
|
|
293
|
+
for (let i = paragraphIndexAtOffset(doc, a); i < doc.childCount && starts[i] <= b; i++) {
|
|
294
|
+
const ps = starts[i];
|
|
295
|
+
const pe = starts[i + 1] - 1;
|
|
296
|
+
const s = Math.max(a, ps);
|
|
297
|
+
const e = Math.min(b, pe);
|
|
298
|
+
if (e > s) {
|
|
299
|
+
for (const r of renderer.rectsForRange(handle, s, e))
|
|
300
|
+
out.push({ x: o.x + r.x, y: o.y + r.y, w: r.w, h: r.h });
|
|
301
|
+
}
|
|
302
|
+
else if (pe === ps && a < b && a <= ps) {
|
|
303
|
+
const line = lines[lineIndexIn(lines, ps)];
|
|
304
|
+
out.push({ x: o.x + line.left, y: o.y + line.top, w: EMPTY_LINE_RECT_W, h: line.height });
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return out;
|
|
308
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { type SinkMirrorDom } from "./contextMirror.js";
|
|
2
|
+
import type { InputTarget } from "./types.js";
|
|
3
|
+
export interface SinkEvent {
|
|
4
|
+
readonly type: string;
|
|
5
|
+
readonly timeStamp: number;
|
|
6
|
+
preventDefault(): void;
|
|
7
|
+
}
|
|
8
|
+
export interface SinkKeyboardEvent extends SinkEvent {
|
|
9
|
+
readonly key: string;
|
|
10
|
+
readonly code: string;
|
|
11
|
+
readonly shiftKey: boolean;
|
|
12
|
+
readonly ctrlKey: boolean;
|
|
13
|
+
readonly altKey: boolean;
|
|
14
|
+
readonly metaKey: boolean;
|
|
15
|
+
readonly keyCode: number;
|
|
16
|
+
readonly isComposing: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface SinkStaticRange {
|
|
19
|
+
readonly startContainer: unknown;
|
|
20
|
+
readonly startOffset: number;
|
|
21
|
+
readonly endContainer: unknown;
|
|
22
|
+
readonly endOffset: number;
|
|
23
|
+
}
|
|
24
|
+
export interface SinkInputEvent extends SinkEvent {
|
|
25
|
+
readonly inputType: string;
|
|
26
|
+
readonly data: string | null;
|
|
27
|
+
readonly dataTransfer: {
|
|
28
|
+
getData(format: string): string;
|
|
29
|
+
} | null;
|
|
30
|
+
readonly cancelable?: boolean;
|
|
31
|
+
getTargetRanges?(): readonly SinkStaticRange[];
|
|
32
|
+
readonly isComposing?: boolean;
|
|
33
|
+
}
|
|
34
|
+
export interface SinkCompositionEvent extends SinkEvent {
|
|
35
|
+
readonly data: string;
|
|
36
|
+
}
|
|
37
|
+
export interface SinkClipboardEvent extends SinkEvent {
|
|
38
|
+
readonly clipboardData: {
|
|
39
|
+
getData(format: string): string;
|
|
40
|
+
setData(format: string, data: string): void;
|
|
41
|
+
} | null;
|
|
42
|
+
}
|
|
43
|
+
export interface SinkFocusEvent extends SinkEvent {
|
|
44
|
+
readonly relatedTarget: EventTarget | null;
|
|
45
|
+
}
|
|
46
|
+
export interface SinkElementLike {
|
|
47
|
+
readonly style: {
|
|
48
|
+
cssText: string;
|
|
49
|
+
left: string;
|
|
50
|
+
top: string;
|
|
51
|
+
height: string;
|
|
52
|
+
};
|
|
53
|
+
textContent: string | null;
|
|
54
|
+
setAttribute(name: string, value: string): void;
|
|
55
|
+
addEventListener(type: string, listener: (e: SinkEvent) => void): void;
|
|
56
|
+
removeEventListener(type: string, listener: (e: SinkEvent) => void): void;
|
|
57
|
+
focus(options?: {
|
|
58
|
+
preventScroll?: boolean;
|
|
59
|
+
}): void;
|
|
60
|
+
remove(): void;
|
|
61
|
+
}
|
|
62
|
+
export interface SinkHostLike {
|
|
63
|
+
appendChild(child: unknown): unknown;
|
|
64
|
+
}
|
|
65
|
+
export interface InputSinkOptions {
|
|
66
|
+
handleEvent?: (event: SinkEvent) => boolean;
|
|
67
|
+
createElement?: () => SinkElementLike;
|
|
68
|
+
safari?: boolean;
|
|
69
|
+
isActiveElement?: (element: SinkElementLike) => boolean;
|
|
70
|
+
keepsSession?: (relatedTarget: EventTarget | null) => boolean;
|
|
71
|
+
virtualKeyboard?: boolean;
|
|
72
|
+
mirrorDom?: (element: SinkElementLike) => SinkMirrorDom;
|
|
73
|
+
local?: (client: {
|
|
74
|
+
x: number;
|
|
75
|
+
y: number;
|
|
76
|
+
}) => {
|
|
77
|
+
x: number;
|
|
78
|
+
y: number;
|
|
79
|
+
};
|
|
80
|
+
bounds?: () => {
|
|
81
|
+
x: number;
|
|
82
|
+
y: number;
|
|
83
|
+
w: number;
|
|
84
|
+
h: number;
|
|
85
|
+
} | null;
|
|
86
|
+
}
|
|
87
|
+
export declare class InputSink {
|
|
88
|
+
private readonly host;
|
|
89
|
+
private readonly target;
|
|
90
|
+
readonly element: SinkElementLike;
|
|
91
|
+
private readonly listeners;
|
|
92
|
+
private readonly safari;
|
|
93
|
+
private readonly isActiveElement;
|
|
94
|
+
private readonly keepsSession;
|
|
95
|
+
private readonly mirror;
|
|
96
|
+
private readonly dom;
|
|
97
|
+
private attached;
|
|
98
|
+
private composing;
|
|
99
|
+
private compositionEndedAt;
|
|
100
|
+
private suppressBlur;
|
|
101
|
+
private pasteGuard;
|
|
102
|
+
private pasteTimer;
|
|
103
|
+
private written;
|
|
104
|
+
private writtenVersion;
|
|
105
|
+
private unaligned;
|
|
106
|
+
private readonly local;
|
|
107
|
+
private readonly bounds;
|
|
108
|
+
private composition;
|
|
109
|
+
private disconnect;
|
|
110
|
+
private holding;
|
|
111
|
+
private syncing;
|
|
112
|
+
private released;
|
|
113
|
+
private releasedComposition;
|
|
114
|
+
private releaseTimer;
|
|
115
|
+
private pending;
|
|
116
|
+
private frozen;
|
|
117
|
+
constructor(host: SinkHostLike, target: InputTarget, options?: InputSinkOptions);
|
|
118
|
+
attach(): void;
|
|
119
|
+
detach(): void;
|
|
120
|
+
focus(): void;
|
|
121
|
+
updatePosition(): void;
|
|
122
|
+
poll(): void;
|
|
123
|
+
commitComposition(): boolean;
|
|
124
|
+
private onKeyDown;
|
|
125
|
+
private onBeforeInput;
|
|
126
|
+
private onInput;
|
|
127
|
+
private onCompositionStart;
|
|
128
|
+
private onCompositionUpdate;
|
|
129
|
+
private onCompositionEnd;
|
|
130
|
+
private onCopy;
|
|
131
|
+
private onCut;
|
|
132
|
+
private onPaste;
|
|
133
|
+
private onBlur;
|
|
134
|
+
private apply;
|
|
135
|
+
private applyPaste;
|
|
136
|
+
private readClipboard;
|
|
137
|
+
private writeClipboard;
|
|
138
|
+
private clearPasteGuard;
|
|
139
|
+
private rewrite;
|
|
140
|
+
private restoreSelection;
|
|
141
|
+
private readKeyboard;
|
|
142
|
+
private readBack;
|
|
143
|
+
private applyEdits;
|
|
144
|
+
private readBefore;
|
|
145
|
+
private onReleased;
|
|
146
|
+
private endReleasedComposition;
|
|
147
|
+
private writeBackLater;
|
|
148
|
+
private sync;
|
|
149
|
+
private targetOffsets;
|
|
150
|
+
private targetFlat;
|
|
151
|
+
private replaceByTarget;
|
|
152
|
+
private deleteByTarget;
|
|
153
|
+
private composeAt;
|
|
154
|
+
private followComposition;
|
|
155
|
+
private finishComposition;
|
|
156
|
+
private commitShown;
|
|
157
|
+
}
|