@aicayzer/inkkit 0.0.6 → 0.0.7
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 +2 -0
- package/dist/callouts.d.ts +2 -0
- package/dist/callouts.js +11 -0
- package/dist/dialect.js +10 -2
- package/dist/editor.d.ts +25 -3
- package/dist/editor.js +451 -96
- package/dist/index.d.ts +2 -1
- package/dist/outline.d.ts +22 -0
- package/dist/outline.js +79 -0
- package/dist/search.d.ts +14 -0
- package/dist/search.js +171 -0
- package/dist/source.d.ts +17 -0
- package/dist/source.js +117 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { InkKitEditor } from './editor.js';
|
|
2
|
-
export type { EditorEvents, CaretState, Mark, Block, FormatCommand, Keymap, } from './editor.js';
|
|
2
|
+
export type { EditorEvents, EditingMode, CaretState, Mark, Block, FormatCommand, Keymap, } from './editor.js';
|
|
3
3
|
export type { TableCommand, TableOptions } from './tables.js';
|
|
4
|
+
export type { Heading } from './outline.js';
|
|
4
5
|
export { InkKitError } from './types.js';
|
|
5
6
|
export type { DocumentFormat, DocumentContext, DocumentInput, DocumentSnapshot, EditorErrorCode, ImageAdapter, CapturedImage, PortableImage, ClipboardInput, ClipboardOutput, ClipboardImage, ClipboardDiagram, PrintableDocument, PrintableWarning, } from './types.js';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Ctx } from '@milkdown/kit/ctx';
|
|
2
|
+
import type { Node as ProseNode } from '@milkdown/kit/prose/model';
|
|
3
|
+
import type { EditorView } from '@milkdown/kit/prose/view';
|
|
4
|
+
export interface Heading {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly level: number;
|
|
7
|
+
readonly text: string;
|
|
8
|
+
readonly documentId: string;
|
|
9
|
+
readonly generation: number;
|
|
10
|
+
readonly revision: number;
|
|
11
|
+
}
|
|
12
|
+
export interface OutlineContext {
|
|
13
|
+
documentId: string;
|
|
14
|
+
generation: number;
|
|
15
|
+
revision: number;
|
|
16
|
+
epoch: number;
|
|
17
|
+
mode: 'source' | 'formatted';
|
|
18
|
+
}
|
|
19
|
+
export declare function collectFormattedHeadings(doc: ProseNode, context: OutlineContext): readonly Heading[];
|
|
20
|
+
export declare function collectSourceHeadings(ctx: Ctx, source: string, context: OutlineContext): readonly Heading[];
|
|
21
|
+
export declare function sourceHeadingPosition(ctx: Ctx, source: string, requested: Heading, context: OutlineContext): number;
|
|
22
|
+
export declare function navigateFormattedHeading(view: EditorView, requested: Heading, context: OutlineContext): boolean;
|
package/dist/outline.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { parserCtx, remarkCtx } from '@milkdown/kit/core';
|
|
2
|
+
import { TextSelection } from '@milkdown/kit/prose/state';
|
|
3
|
+
import { clipboardText } from './clipboard.js';
|
|
4
|
+
import { revealCalloutAncestors } from './callouts.js';
|
|
5
|
+
import { InkKitError } from './types.js';
|
|
6
|
+
function formattedLocations(doc) {
|
|
7
|
+
const headings = [];
|
|
8
|
+
doc.descendants((node, position) => {
|
|
9
|
+
if (node.type.name === 'heading')
|
|
10
|
+
headings.push({ node, position });
|
|
11
|
+
});
|
|
12
|
+
return headings;
|
|
13
|
+
}
|
|
14
|
+
function sourceLocations(ctx, source) {
|
|
15
|
+
const processor = ctx.get(remarkCtx);
|
|
16
|
+
const tree = processor.runSync(processor.parse(source), {
|
|
17
|
+
value: source,
|
|
18
|
+
});
|
|
19
|
+
const positions = [];
|
|
20
|
+
const bom = source.startsWith('\uFEFF') ? 1 : 0;
|
|
21
|
+
const visit = (node) => {
|
|
22
|
+
if (node.type === 'heading' && node.position?.start.offset != null)
|
|
23
|
+
positions.push({
|
|
24
|
+
position: node.position.start.offset + bom,
|
|
25
|
+
level: node.depth,
|
|
26
|
+
});
|
|
27
|
+
node.children?.forEach(visit);
|
|
28
|
+
};
|
|
29
|
+
visit(tree);
|
|
30
|
+
const parsed = formattedLocations(ctx.get(parserCtx)(source));
|
|
31
|
+
if (positions.length !== parsed.length ||
|
|
32
|
+
positions.some((item, index) => item.level !== parsed[index].node.attrs.level))
|
|
33
|
+
throw new InkKitError('preservation', 'The current Markdown headings cannot be located safely');
|
|
34
|
+
return parsed.map((heading, index) => ({
|
|
35
|
+
...heading,
|
|
36
|
+
position: positions[index].position,
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
function entry(heading, context) {
|
|
40
|
+
return Object.freeze({
|
|
41
|
+
id: `heading:${context.epoch}:${context.mode}:${context.generation}:${context.revision}:${heading.position}`,
|
|
42
|
+
level: Number(heading.node.attrs.level),
|
|
43
|
+
text: clipboardText(heading.node.content),
|
|
44
|
+
documentId: context.documentId,
|
|
45
|
+
generation: context.generation,
|
|
46
|
+
revision: context.revision,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
export function collectFormattedHeadings(doc, context) {
|
|
50
|
+
return Object.freeze(formattedLocations(doc).map((item) => entry(item, context)));
|
|
51
|
+
}
|
|
52
|
+
export function collectSourceHeadings(ctx, source, context) {
|
|
53
|
+
return Object.freeze(sourceLocations(ctx, source).map((item) => entry(item, context)));
|
|
54
|
+
}
|
|
55
|
+
function currentLocation(locations, requested, context) {
|
|
56
|
+
for (const location of locations) {
|
|
57
|
+
const current = entry(location, context);
|
|
58
|
+
if (current.id === requested.id &&
|
|
59
|
+
current.documentId === requested.documentId &&
|
|
60
|
+
current.generation === requested.generation &&
|
|
61
|
+
current.revision === requested.revision &&
|
|
62
|
+
current.level === requested.level &&
|
|
63
|
+
current.text === requested.text)
|
|
64
|
+
return location.position;
|
|
65
|
+
}
|
|
66
|
+
throw new InkKitError('stale-document', 'The heading is no longer current');
|
|
67
|
+
}
|
|
68
|
+
export function sourceHeadingPosition(ctx, source, requested, context) {
|
|
69
|
+
return currentLocation(sourceLocations(ctx, source), requested, context);
|
|
70
|
+
}
|
|
71
|
+
export function navigateFormattedHeading(view, requested, context) {
|
|
72
|
+
const position = currentLocation(formattedLocations(view.state.doc), requested, context);
|
|
73
|
+
revealCalloutAncestors(view, position + 1);
|
|
74
|
+
view.dispatch(view.state.tr
|
|
75
|
+
.setSelection(TextSelection.create(view.state.doc, position + 1))
|
|
76
|
+
.scrollIntoView());
|
|
77
|
+
view.focus();
|
|
78
|
+
return true;
|
|
79
|
+
}
|
package/dist/search.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Node as ProseNode } from '@milkdown/kit/prose/model';
|
|
2
|
+
import type { EditorView } from '@milkdown/kit/prose/view';
|
|
3
|
+
export interface TextMatch {
|
|
4
|
+
from: number;
|
|
5
|
+
to: number;
|
|
6
|
+
}
|
|
7
|
+
export interface LiteralReplacement extends TextMatch {
|
|
8
|
+
text: string;
|
|
9
|
+
count: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function findLiteral(text: string, search: string, from?: number): TextMatch | undefined;
|
|
12
|
+
export declare function replaceLiteral(text: string, search: string, replacement: string, selection: TextMatch, all?: boolean): LiteralReplacement;
|
|
13
|
+
export declare function findFormatted(view: EditorView, text: string): void;
|
|
14
|
+
export declare function replaceFormatted(view: EditorView, search: string, replacement: string, preflight: (doc: ProseNode) => void, all?: boolean): number;
|
package/dist/search.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { Fragment, Slice } from '@milkdown/kit/prose/model';
|
|
2
|
+
import { closeHistory } from '@milkdown/kit/prose/history';
|
|
3
|
+
import { Selection, TextSelection } from '@milkdown/kit/prose/state';
|
|
4
|
+
import { SearchQuery, setSearchState, getSearchState } from 'prosemirror-search';
|
|
5
|
+
import { InkKitError } from './types.js';
|
|
6
|
+
function escapedPattern(text) {
|
|
7
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
8
|
+
}
|
|
9
|
+
function literalMatches(text, search) {
|
|
10
|
+
if (!search)
|
|
11
|
+
return [];
|
|
12
|
+
const pattern = new RegExp(escapedPattern(search), 'giu');
|
|
13
|
+
return [...text.matchAll(pattern)].map((match) => ({
|
|
14
|
+
from: match.index,
|
|
15
|
+
to: match.index + match[0].length,
|
|
16
|
+
}));
|
|
17
|
+
}
|
|
18
|
+
export function findLiteral(text, search, from = 0) {
|
|
19
|
+
const matches = literalMatches(text, search);
|
|
20
|
+
return matches.find((match) => match.from >= from) ?? matches[0];
|
|
21
|
+
}
|
|
22
|
+
export function replaceLiteral(text, search, replacement, selection, all = false) {
|
|
23
|
+
const matches = literalMatches(text, search);
|
|
24
|
+
const selected = matches.find((match) => match.from === selection.from && match.to === selection.to) ??
|
|
25
|
+
matches.find((match) => match.from >= selection.to) ??
|
|
26
|
+
matches[0];
|
|
27
|
+
const changed = (all ? matches : selected ? [selected] : []).filter((match) => text.slice(match.from, match.to) !== replacement);
|
|
28
|
+
if (!changed.length)
|
|
29
|
+
return { ...selection, text, count: 0 };
|
|
30
|
+
let output = text;
|
|
31
|
+
for (const match of [...changed].reverse())
|
|
32
|
+
output = output.slice(0, match.from) + replacement + output.slice(match.to);
|
|
33
|
+
if (!all)
|
|
34
|
+
return {
|
|
35
|
+
text: output,
|
|
36
|
+
count: 1,
|
|
37
|
+
from: changed[0].from,
|
|
38
|
+
to: changed[0].from + replacement.length,
|
|
39
|
+
};
|
|
40
|
+
const map = (position) => {
|
|
41
|
+
let difference = 0;
|
|
42
|
+
for (const match of changed) {
|
|
43
|
+
if (position < match.from)
|
|
44
|
+
break;
|
|
45
|
+
if (position <= match.to)
|
|
46
|
+
return (match.from +
|
|
47
|
+
difference +
|
|
48
|
+
(position === match.from ? 0 : replacement.length));
|
|
49
|
+
difference += replacement.length - (match.to - match.from);
|
|
50
|
+
}
|
|
51
|
+
return position + difference;
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
text: output,
|
|
55
|
+
count: changed.length,
|
|
56
|
+
from: map(selection.from),
|
|
57
|
+
to: map(selection.to),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function formattedQuery(text) {
|
|
61
|
+
// Unicode regexp matching retains offsets when lowercasing would expand a character.
|
|
62
|
+
return new SearchQuery({
|
|
63
|
+
search: escapedPattern(text),
|
|
64
|
+
regexp: true,
|
|
65
|
+
literal: true,
|
|
66
|
+
filter: (state, match) => {
|
|
67
|
+
const from = state.doc.resolve(match.from);
|
|
68
|
+
const to = state.doc.resolve(match.to);
|
|
69
|
+
if (from.parent !== to.parent || !from.parent.inlineContent)
|
|
70
|
+
return false;
|
|
71
|
+
const content = state.doc.slice(match.from, match.to).content;
|
|
72
|
+
let textOnly = true;
|
|
73
|
+
content.forEach((node) => {
|
|
74
|
+
if (!node.isText)
|
|
75
|
+
textOnly = false;
|
|
76
|
+
});
|
|
77
|
+
return textOnly;
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
export function findFormatted(view, text) {
|
|
82
|
+
const query = formattedQuery(text);
|
|
83
|
+
const previous = getSearchState(view.state)?.query.search;
|
|
84
|
+
let tr = setSearchState(view.state.tr, query);
|
|
85
|
+
if (text) {
|
|
86
|
+
const start = previous === query.search
|
|
87
|
+
? view.state.selection.to
|
|
88
|
+
: view.state.selection.from;
|
|
89
|
+
const match = query.findNext(view.state, start) ?? query.findNext(view.state, 0);
|
|
90
|
+
if (match)
|
|
91
|
+
tr = tr
|
|
92
|
+
.setSelection(TextSelection.create(tr.doc, match.from, match.to))
|
|
93
|
+
.scrollIntoView();
|
|
94
|
+
else
|
|
95
|
+
tr = tr.setSelection(Selection.near(view.state.selection.$to));
|
|
96
|
+
}
|
|
97
|
+
else if (previous)
|
|
98
|
+
tr = tr.setSelection(Selection.near(view.state.selection.$to));
|
|
99
|
+
view.dispatch(tr);
|
|
100
|
+
}
|
|
101
|
+
export function replaceFormatted(view, search, replacement, preflight, all = false) {
|
|
102
|
+
if (!search)
|
|
103
|
+
return 0;
|
|
104
|
+
const state = view.state;
|
|
105
|
+
const query = formattedQuery(search);
|
|
106
|
+
if (!query.valid)
|
|
107
|
+
return 0;
|
|
108
|
+
const matches = [];
|
|
109
|
+
if (all) {
|
|
110
|
+
for (let position = 0;;) {
|
|
111
|
+
const match = query.findNext(state, position);
|
|
112
|
+
if (!match)
|
|
113
|
+
break;
|
|
114
|
+
matches.push(match);
|
|
115
|
+
position = match.to;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
const selection = state.selection;
|
|
120
|
+
const current = query.findNext(state, selection.from, selection.to);
|
|
121
|
+
const match = (current?.from === selection.from && current.to === selection.to
|
|
122
|
+
? current
|
|
123
|
+
: undefined) ??
|
|
124
|
+
query.findNext(state, selection.to) ??
|
|
125
|
+
query.findNext(state, 0);
|
|
126
|
+
if (match)
|
|
127
|
+
matches.push(match);
|
|
128
|
+
}
|
|
129
|
+
const value = replacement.replace(/\r\n?/g, '\n');
|
|
130
|
+
const changed = matches.filter((match) => state.doc.textBetween(match.from, match.to) !== value);
|
|
131
|
+
if (!changed.length)
|
|
132
|
+
return 0;
|
|
133
|
+
let tr = setSearchState(state.tr, query);
|
|
134
|
+
for (const match of [...changed].reverse()) {
|
|
135
|
+
const from = state.doc.resolve(match.from);
|
|
136
|
+
const to = state.doc.resolve(match.to);
|
|
137
|
+
const marks = from.marksAcross(to) ?? from.marks();
|
|
138
|
+
let content = Fragment.empty;
|
|
139
|
+
if (from.parent.type.spec.code) {
|
|
140
|
+
if (value)
|
|
141
|
+
content = Fragment.from(state.schema.text(value, marks));
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
const nodes = [];
|
|
145
|
+
const lines = value.split('\n');
|
|
146
|
+
lines.forEach((line, index) => {
|
|
147
|
+
if (index)
|
|
148
|
+
nodes.push(state.schema.nodes.hardbreak.create({ isHTML: index === lines.length - 1 && line === '' }, null, marks));
|
|
149
|
+
if (line)
|
|
150
|
+
nodes.push(state.schema.text(line, marks));
|
|
151
|
+
});
|
|
152
|
+
content = Fragment.fromArray(nodes);
|
|
153
|
+
}
|
|
154
|
+
tr.replace(match.from, match.to, new Slice(content, 0, 0));
|
|
155
|
+
}
|
|
156
|
+
if (!all) {
|
|
157
|
+
const match = changed[0];
|
|
158
|
+
tr = tr.setSelection(TextSelection.create(tr.doc, match.from, tr.mapping.map(match.to, 1)));
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
preflight(tr.doc);
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
if (error instanceof InkKitError)
|
|
165
|
+
throw error;
|
|
166
|
+
throw new InkKitError('preservation', error instanceof Error ? error.message : 'Cannot preserve replacement');
|
|
167
|
+
}
|
|
168
|
+
view.dispatch(closeHistory(tr).scrollIntoView());
|
|
169
|
+
view.dispatch(closeHistory(view.state.tr).setMeta('addToHistory', false));
|
|
170
|
+
return changed.length;
|
|
171
|
+
}
|
package/dist/source.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Node as ProseNode } from '@milkdown/kit/prose/model';
|
|
2
|
+
export declare const sourceAttribute = "inkkitSource";
|
|
3
|
+
export interface SourceProvenance {
|
|
4
|
+
readonly text: string;
|
|
5
|
+
readonly format: 'md' | 'txt';
|
|
6
|
+
readonly from: number;
|
|
7
|
+
readonly to: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function cleanSourceDoc(doc: ProseNode): ProseNode;
|
|
10
|
+
export declare function rawOffset(source: string, normalized: number): number;
|
|
11
|
+
export declare function normalizedOffset(source: string, raw: number): number;
|
|
12
|
+
export declare function editedPlainSource(previous: string, value: string): string;
|
|
13
|
+
export declare function sourceSelection(source: string, start: number, end: number): {
|
|
14
|
+
markdown: string;
|
|
15
|
+
shareable: string;
|
|
16
|
+
};
|
|
17
|
+
export declare function revealSourceCaret(plain: HTMLTextAreaElement): void;
|
package/dist/source.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { literalCommentSpans } from './comments.js';
|
|
2
|
+
export const sourceAttribute = 'inkkitSource';
|
|
3
|
+
export function cleanSourceDoc(doc) {
|
|
4
|
+
return doc.attrs[sourceAttribute] == null
|
|
5
|
+
? doc
|
|
6
|
+
: doc.type.create({ ...doc.attrs, [sourceAttribute]: null }, doc.content);
|
|
7
|
+
}
|
|
8
|
+
export function rawOffset(source, normalized) {
|
|
9
|
+
let position = 0, count = 0;
|
|
10
|
+
while (position < source.length && count < normalized) {
|
|
11
|
+
if (source[position] === '\r' && source[position + 1] === '\n')
|
|
12
|
+
position++;
|
|
13
|
+
position++;
|
|
14
|
+
count++;
|
|
15
|
+
}
|
|
16
|
+
return position;
|
|
17
|
+
}
|
|
18
|
+
export function normalizedOffset(source, raw) {
|
|
19
|
+
return source.slice(0, raw).replace(/\r\n?/g, '\n').length;
|
|
20
|
+
}
|
|
21
|
+
export function editedPlainSource(previous, value) {
|
|
22
|
+
const normalized = previous.replace(/\r\n?/g, '\n');
|
|
23
|
+
if (normalized === value)
|
|
24
|
+
return previous;
|
|
25
|
+
let prefix = 0, suffix = 0;
|
|
26
|
+
while (prefix < normalized.length &&
|
|
27
|
+
prefix < value.length &&
|
|
28
|
+
normalized[prefix] === value[prefix])
|
|
29
|
+
prefix++;
|
|
30
|
+
while (suffix < normalized.length - prefix &&
|
|
31
|
+
suffix < value.length - prefix &&
|
|
32
|
+
normalized[normalized.length - suffix - 1] ===
|
|
33
|
+
value[value.length - suffix - 1])
|
|
34
|
+
suffix++;
|
|
35
|
+
const ending = /\r\n|\r|\n/.exec(previous)?.[0] ?? '\n';
|
|
36
|
+
return (previous.slice(0, rawOffset(previous, prefix)) +
|
|
37
|
+
value
|
|
38
|
+
.slice(prefix, value.length - suffix)
|
|
39
|
+
.replace(/\r\n?/g, '\n')
|
|
40
|
+
.replaceAll('\n', ending) +
|
|
41
|
+
previous.slice(rawOffset(previous, normalized.length - suffix)));
|
|
42
|
+
}
|
|
43
|
+
export function sourceSelection(source, start, end) {
|
|
44
|
+
const from = rawOffset(source, start), to = rawOffset(source, end);
|
|
45
|
+
let shareable = '', cursor = from;
|
|
46
|
+
const bom = source.startsWith('\uFEFF') ? 1 : 0;
|
|
47
|
+
for (const parsed of literalCommentSpans(source.slice(bom))) {
|
|
48
|
+
const span = { start: parsed.start + bom, end: parsed.end + bom };
|
|
49
|
+
if (span.end <= from || span.start >= to)
|
|
50
|
+
continue;
|
|
51
|
+
if (span.start > cursor)
|
|
52
|
+
shareable += source.slice(cursor, Math.min(span.start, to));
|
|
53
|
+
cursor = Math.max(cursor, Math.min(span.end, to));
|
|
54
|
+
}
|
|
55
|
+
shareable += source.slice(cursor, to);
|
|
56
|
+
return { markdown: source.slice(from, to), shareable };
|
|
57
|
+
}
|
|
58
|
+
export function revealSourceCaret(plain) {
|
|
59
|
+
if (!plain.clientWidth || !plain.clientHeight)
|
|
60
|
+
return;
|
|
61
|
+
const document = plain.ownerDocument;
|
|
62
|
+
const style = document.defaultView.getComputedStyle(plain);
|
|
63
|
+
const mirror = document.createElement('div');
|
|
64
|
+
for (const property of [
|
|
65
|
+
'font',
|
|
66
|
+
'font-family',
|
|
67
|
+
'font-size',
|
|
68
|
+
'font-weight',
|
|
69
|
+
'font-style',
|
|
70
|
+
'font-stretch',
|
|
71
|
+
'font-variant',
|
|
72
|
+
'line-height',
|
|
73
|
+
'letter-spacing',
|
|
74
|
+
'word-spacing',
|
|
75
|
+
'tab-size',
|
|
76
|
+
'padding',
|
|
77
|
+
'text-indent',
|
|
78
|
+
'text-transform',
|
|
79
|
+
'direction',
|
|
80
|
+
'word-break',
|
|
81
|
+
])
|
|
82
|
+
mirror.style.setProperty(property, style.getPropertyValue(property));
|
|
83
|
+
Object.assign(mirror.style, {
|
|
84
|
+
position: 'fixed',
|
|
85
|
+
visibility: 'hidden',
|
|
86
|
+
left: '-10000px',
|
|
87
|
+
top: '0',
|
|
88
|
+
width: `${plain.clientWidth}px`,
|
|
89
|
+
boxSizing: 'border-box',
|
|
90
|
+
whiteSpace: plain.wrap === 'off' ? 'pre' : 'pre-wrap',
|
|
91
|
+
overflowWrap: plain.wrap === 'off' ? 'normal' : 'break-word',
|
|
92
|
+
});
|
|
93
|
+
mirror.setAttribute('aria-hidden', 'true');
|
|
94
|
+
const caret = document.createElement('span');
|
|
95
|
+
caret.setAttribute('data-inkkit-source-caret', '');
|
|
96
|
+
caret.textContent = plain.value.slice(plain.selectionStart) || '\u200b';
|
|
97
|
+
mirror.append(document.createTextNode(plain.value.slice(0, plain.selectionStart)), caret);
|
|
98
|
+
document.body.append(mirror);
|
|
99
|
+
try {
|
|
100
|
+
// Textareas expose selection offsets but no caret geometry; matching their layout also accounts for wrapped lines.
|
|
101
|
+
const rect = caret.getClientRects()[0] ?? caret.getBoundingClientRect();
|
|
102
|
+
const top = rect.top - mirror.getBoundingClientRect().top;
|
|
103
|
+
const lineHeight = Math.max(rect.height, Number.parseFloat(style.lineHeight) || rect.height);
|
|
104
|
+
const insetTop = Number.parseFloat(style.paddingTop) || 0;
|
|
105
|
+
const insetBottom = Number.parseFloat(style.paddingBottom) || 0;
|
|
106
|
+
if (plain.selectionStart === 0)
|
|
107
|
+
plain.scrollTop = 0;
|
|
108
|
+
else if (top < plain.scrollTop + insetTop)
|
|
109
|
+
plain.scrollTop = Math.max(0, top - insetTop);
|
|
110
|
+
else if (top + lineHeight >
|
|
111
|
+
plain.scrollTop + plain.clientHeight - insetBottom)
|
|
112
|
+
plain.scrollTop = Math.max(0, top + lineHeight - plain.clientHeight + insetBottom);
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
mirror.remove();
|
|
116
|
+
}
|
|
117
|
+
}
|