svedit 0.2.0 → 0.4.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 +41 -21
- package/dist/AnnotatedTextProperty.svelte +117 -0
- package/dist/AnnotatedTextProperty.svelte.d.ts +7 -0
- package/dist/AnnotatedTextProperty.svelte.d.ts.map +1 -0
- package/dist/CustomProperty.svelte +20 -12
- package/dist/CustomProperty.svelte.d.ts +1 -2
- package/dist/CustomProperty.svelte.d.ts.map +1 -1
- package/dist/Document.svelte.d.ts +40 -17
- package/dist/Document.svelte.d.ts.map +1 -1
- package/dist/Document.svelte.js +192 -77
- package/dist/Node.svelte +18 -14
- package/dist/Node.svelte.d.ts.map +1 -1
- package/dist/NodeArrayProperty.svelte +1 -1
- package/dist/Svedit.svelte +628 -109
- package/dist/Svedit.svelte.d.ts +1 -1
- package/dist/Svedit.svelte.d.ts.map +1 -1
- package/dist/Transaction.svelte.d.ts +4 -7
- package/dist/Transaction.svelte.d.ts.map +1 -1
- package/dist/Transaction.svelte.js +74 -81
- package/dist/commands.svelte.d.ts.map +1 -1
- package/dist/commands.svelte.js +20 -10
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/types.d.ts +66 -21
- package/dist/util.d.ts +125 -3
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +191 -34
- package/package.json +1 -1
- package/src/lib/AnnotatedTextProperty.svelte +117 -0
- package/src/lib/CustomProperty.svelte +20 -12
- package/src/lib/Document.svelte.js +192 -77
- package/src/lib/Node.svelte +18 -14
- package/src/lib/NodeArrayProperty.svelte +1 -1
- package/src/lib/Svedit.svelte +628 -109
- package/src/lib/Transaction.svelte.js +74 -81
- package/src/lib/commands.svelte.js +20 -10
- package/src/lib/index.js +1 -1
- package/src/lib/types.d.ts +66 -21
- package/src/lib/util.js +191 -34
- package/dist/AnnotatedStringProperty.svelte +0 -126
- package/dist/AnnotatedStringProperty.svelte.d.ts +0 -7
- package/dist/AnnotatedStringProperty.svelte.d.ts.map +0 -1
- package/src/lib/AnnotatedStringProperty.svelte +0 -126
package/src/lib/util.js
CHANGED
|
@@ -1,75 +1,232 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// ["Hello wo", [[6,8, "strong"]]]
|
|
5
|
-
// ["rld", [[0,3, "strong"]]]
|
|
6
|
-
// ]
|
|
7
|
-
export function split_annotated_string(text_with_annotations, at_position) {
|
|
8
|
-
const [text, annotations] = text_with_annotations;
|
|
9
|
-
|
|
10
|
-
// Split the text
|
|
11
|
-
const left_text = text.slice(0, at_position);
|
|
12
|
-
const right_text = text.slice(at_position);
|
|
1
|
+
/**
|
|
2
|
+
* @import { Annotation, AnnotatedText } from './types.d.ts';
|
|
3
|
+
*/
|
|
13
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Get the actual character length (accounting for multi-byte characters)
|
|
7
|
+
*
|
|
8
|
+
* Uses Intl.Segmenter to count grapheme clusters rather than UTF-16 code units,
|
|
9
|
+
* ensuring emojis and other complex Unicode sequences are counted as single characters.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} str - The string to measure
|
|
12
|
+
* @returns {number} The number of visual characters (grapheme clusters)
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* get_char_length('Hello') // Returns: 5
|
|
16
|
+
* get_char_length('a😀b') // Returns: 3 (not 4)
|
|
17
|
+
* get_char_length('👋🏽') // Returns: 1 (not 4 - skin tone modifier treated as single char)
|
|
18
|
+
*/
|
|
19
|
+
export function get_char_length(str) {
|
|
20
|
+
const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' });
|
|
21
|
+
return [...segmenter.segment(str)].length;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Get a single character at the specified position (accounting for multi-byte characters)
|
|
26
|
+
*
|
|
27
|
+
* Uses Intl.Segmenter to access grapheme clusters rather than UTF-16 code units,
|
|
28
|
+
* ensuring emojis and other complex Unicode sequences are treated as single characters.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} str - The string to access
|
|
31
|
+
* @param {number} index - Character position (0-based)
|
|
32
|
+
* @returns {string} The character at the specified position, or empty string if index is out of bounds
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* get_char_at('Hello', 1) // Returns: 'e'
|
|
36
|
+
* get_char_at('a😀b', 1) // Returns: '😀' (full emoji)
|
|
37
|
+
* get_char_at('👋🏽', 0) // Returns: '👋🏽' (skin tone modifier included)
|
|
38
|
+
*/
|
|
39
|
+
export function get_char_at(str, index) {
|
|
40
|
+
const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' });
|
|
41
|
+
const segments = [...segmenter.segment(str)];
|
|
42
|
+
return segments[index].segment;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Slice string by character positions (accounting for multi-byte characters)
|
|
47
|
+
*
|
|
48
|
+
* Uses Intl.Segmenter to slice by grapheme clusters rather than UTF-16 code units,
|
|
49
|
+
* ensuring emojis and other complex Unicode sequences remain intact.
|
|
50
|
+
*
|
|
51
|
+
* @param {string} str - The string to slice
|
|
52
|
+
* @param {number} start - Starting character position (inclusive)
|
|
53
|
+
* @param {number} [end] - Ending character position (exclusive). If undefined, slices to end
|
|
54
|
+
* @returns {string} The sliced string with complete characters
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* char_slice('Hello 😀 World', 6, 8) // Returns: '😀 ' (emoji stays intact)
|
|
58
|
+
* char_slice('a👋🏽b', 1, 2) // Returns: '👋🏽' (skin tone modifier included)
|
|
59
|
+
*/
|
|
60
|
+
export function char_slice(str, start, end = undefined) {
|
|
61
|
+
const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' });
|
|
62
|
+
const segments = [...segmenter.segment(str)];
|
|
63
|
+
return segments.slice(start, end).map(s => s.segment).join('');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Convert UTF-16 code unit offset to grapheme cluster offset within a string
|
|
68
|
+
*
|
|
69
|
+
* Converts DOM selection offsets (which use UTF-16 code units) to character-based
|
|
70
|
+
* offsets (grapheme clusters) for consistent text manipulation.
|
|
71
|
+
*
|
|
72
|
+
* @param {string} str - The string to convert offsets within
|
|
73
|
+
* @param {number} utf16_offset - The UTF-16 code unit offset from DOM
|
|
74
|
+
* @returns {number} The corresponding grapheme cluster offset
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* // For string "a😀b" where 😀 uses 2 UTF-16 code units
|
|
78
|
+
* utf16_to_char_offset("a😀b", 3) // Returns: 2 (position after emoji)
|
|
79
|
+
*/
|
|
80
|
+
export function utf16_to_char_offset(str, utf16_offset) {
|
|
81
|
+
const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' });
|
|
82
|
+
const segments = [...segmenter.segment(str)];
|
|
83
|
+
let char_offset = 0;
|
|
84
|
+
let utf16_count = 0;
|
|
85
|
+
|
|
86
|
+
for (const segment of segments) {
|
|
87
|
+
if (utf16_count >= utf16_offset) break;
|
|
88
|
+
utf16_count += segment.segment.length;
|
|
89
|
+
if (utf16_count > utf16_offset) break;
|
|
90
|
+
char_offset++;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return char_offset;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Convert grapheme cluster offset to UTF-16 code unit offset within a string
|
|
98
|
+
*
|
|
99
|
+
* Converts character-based offsets (grapheme clusters) to DOM selection offsets
|
|
100
|
+
* (UTF-16 code units) for proper DOM selection positioning.
|
|
101
|
+
*
|
|
102
|
+
* @param {string} str - The string to convert offsets within
|
|
103
|
+
* @param {number} char_offset - The grapheme cluster offset
|
|
104
|
+
* @returns {number} The corresponding UTF-16 code unit offset for DOM operations
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* // For string "a😀b" where 😀 uses 2 UTF-16 code units
|
|
108
|
+
* char_to_utf16_offset("a😀b", 2) // Returns: 3 (UTF-16 position after emoji)
|
|
109
|
+
*/
|
|
110
|
+
export function char_to_utf16_offset(str, char_offset) {
|
|
111
|
+
const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' });
|
|
112
|
+
const segments = [...segmenter.segment(str)];
|
|
113
|
+
let utf16_offset = 0;
|
|
114
|
+
|
|
115
|
+
for (let i = 0; i < Math.min(char_offset, segments.length); i++) {
|
|
116
|
+
utf16_offset += segments[i].segment.length;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return utf16_offset;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Splits an annotated text at the specified character position.
|
|
124
|
+
*
|
|
125
|
+
* Annotations that span the split point will be divided appropriately,
|
|
126
|
+
* with offsets adjusted for each resulting part.
|
|
127
|
+
*
|
|
128
|
+
* @param {AnnotatedText} text_with_annotations - Annotated text object
|
|
129
|
+
* @param {number} at_position - Character position where to split (0-based)
|
|
130
|
+
* @returns {[AnnotatedText, AnnotatedText]} Tuple of [left_part, right_part]
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* split_annotated_text({text: "Hello world", annotations: [{start_offset: 6, end_offset: 11, node_id: "strong"}]}, 8)
|
|
134
|
+
* // Returns:
|
|
135
|
+
* // [
|
|
136
|
+
* // {text: "Hello wo", annotations: [{start_offset: 6, end_offset: 8, node_id: "strong"}]},
|
|
137
|
+
* // {text: "rld", annotations: [{start_offset: 0, end_offset: 3, node_id: "strong"}]}
|
|
138
|
+
* // ]
|
|
139
|
+
*/
|
|
140
|
+
export function split_annotated_text(text_with_annotations, at_position) {
|
|
141
|
+
const {text, annotations} = text_with_annotations;
|
|
142
|
+
|
|
143
|
+
// Split the text using character-aware slicing
|
|
144
|
+
const left_text = char_slice(text, 0, at_position);
|
|
145
|
+
const right_text = char_slice(text, at_position);
|
|
146
|
+
|
|
147
|
+
/** @type {Array<Annotation>} */
|
|
14
148
|
const left_annotations = [];
|
|
149
|
+
/** @type {Array<Annotation>} */
|
|
15
150
|
const right_annotations = [];
|
|
16
151
|
|
|
17
152
|
// Process each annotation
|
|
18
|
-
for (const
|
|
19
|
-
if (
|
|
153
|
+
for (const {start_offset, end_offset, node_id} of annotations) {
|
|
154
|
+
if (end_offset <= at_position) {
|
|
20
155
|
// Annotation is entirely in the left part
|
|
21
|
-
left_annotations.push(
|
|
22
|
-
} else if (
|
|
156
|
+
left_annotations.push({start_offset, end_offset, node_id});
|
|
157
|
+
} else if (start_offset >= at_position) {
|
|
23
158
|
// Annotation is entirely in the right part - shift offsets
|
|
24
|
-
right_annotations.push(
|
|
159
|
+
right_annotations.push({start_offset: start_offset - at_position, end_offset: end_offset - at_position, node_id});
|
|
25
160
|
} else {
|
|
26
161
|
// Annotation spans the split point - split it
|
|
27
|
-
left_annotations.push(
|
|
28
|
-
right_annotations.push(
|
|
162
|
+
left_annotations.push({start_offset, end_offset: at_position, node_id});
|
|
163
|
+
right_annotations.push({start_offset: 0, end_offset: end_offset - at_position, node_id});
|
|
29
164
|
}
|
|
30
165
|
}
|
|
31
166
|
|
|
32
167
|
return [
|
|
33
|
-
|
|
34
|
-
|
|
168
|
+
{text: left_text, annotations: left_annotations},
|
|
169
|
+
{text: right_text, annotations: right_annotations}
|
|
35
170
|
];
|
|
36
171
|
}
|
|
37
172
|
|
|
38
173
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
174
|
+
/**
|
|
175
|
+
* Joins two annotated texts into a single annotated text.
|
|
176
|
+
*
|
|
177
|
+
* Annotations from the second text will have their offsets shifted by the length
|
|
178
|
+
* of the first text. Adjacent annotations of the same type and data will be merged.
|
|
179
|
+
*
|
|
180
|
+
* @param {AnnotatedText} first_text - First annotated text object
|
|
181
|
+
* @param {AnnotatedText} second_text - Second annotated text object
|
|
182
|
+
* @returns {AnnotatedText} Combined annotated text object
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* join_annotated_text({text: "Hello wo", annotations: [{start_offset: 6, end_offset: 8, node_id: "strong"}]}, {text: "rld", annotations: [{start_offset: 0, end_offset: 3, node_id: "strong"}]})
|
|
186
|
+
* // Returns: {text: "Hello world", annotations: [{start_offset: 6, end_offset: 11, node_id: "strong"}]}
|
|
187
|
+
*/
|
|
188
|
+
export function join_annotated_text(first_text, second_text) {
|
|
189
|
+
const {text: first_text_content, annotations: first_annotations} = first_text;
|
|
190
|
+
const {text: second_text_content, annotations: second_annotations} = second_text;
|
|
44
191
|
|
|
45
192
|
// Join the text content
|
|
46
193
|
const joined_text = first_text_content + second_text_content;
|
|
47
194
|
|
|
48
195
|
// Start with all annotations from the first text (unchanged)
|
|
196
|
+
/** @type {Array<Annotation>} */
|
|
49
197
|
const joined_annotations = [...first_annotations];
|
|
50
198
|
|
|
51
199
|
// Add annotations from the second text, shifting their offsets
|
|
52
|
-
const offset = first_text_content
|
|
53
|
-
for (const
|
|
54
|
-
|
|
200
|
+
const offset = get_char_length(first_text_content);
|
|
201
|
+
for (const {start_offset, end_offset, node_id} of second_annotations) {
|
|
202
|
+
/** @type {Annotation} */
|
|
203
|
+
const shifted_annotation = {start_offset: start_offset + offset, end_offset: end_offset + offset, node_id};
|
|
55
204
|
|
|
56
205
|
// Check if this annotation can be merged with the last annotation from first text
|
|
57
206
|
const last_annotation = joined_annotations[joined_annotations.length - 1];
|
|
58
207
|
if (last_annotation &&
|
|
59
|
-
last_annotation
|
|
60
|
-
last_annotation
|
|
61
|
-
JSON.stringify(last_annotation[3]) === JSON.stringify(shifted_annotation[3])) { // same data
|
|
208
|
+
last_annotation.end_offset === shifted_annotation.start_offset && // annotations are adjacent
|
|
209
|
+
last_annotation.node_id === shifted_annotation.node_id) { // same node_id
|
|
62
210
|
// Merge by extending the end position of the last annotation
|
|
63
|
-
last_annotation
|
|
211
|
+
last_annotation.end_offset = shifted_annotation.end_offset;
|
|
64
212
|
} else {
|
|
65
213
|
// Add as separate annotation
|
|
66
214
|
joined_annotations.push(shifted_annotation);
|
|
67
215
|
}
|
|
68
216
|
}
|
|
69
217
|
|
|
70
|
-
return
|
|
218
|
+
return {text: joined_text, annotations: joined_annotations};
|
|
71
219
|
}
|
|
72
220
|
|
|
221
|
+
/**
|
|
222
|
+
* Convert snake_case string to PascalCase
|
|
223
|
+
*
|
|
224
|
+
* @param {string} str - The snake_case string to convert
|
|
225
|
+
* @returns {string} The converted PascalCase string
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* snake_to_pascal('list_item') // Returns: 'ListItem'
|
|
229
|
+
*/
|
|
73
230
|
export function snake_to_pascal(str) {
|
|
74
231
|
return str
|
|
75
232
|
.split('_')
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import { getContext } from 'svelte';
|
|
3
|
-
|
|
4
|
-
/** @import { AnnotatedStringPropertyProps, AnnotationFragment } from './types.d.ts'; */
|
|
5
|
-
|
|
6
|
-
const svedit = getContext('svedit');
|
|
7
|
-
|
|
8
|
-
/** @type {AnnotatedStringPropertyProps} */
|
|
9
|
-
let {
|
|
10
|
-
path,
|
|
11
|
-
class: css_class,
|
|
12
|
-
placeholder = '',
|
|
13
|
-
} = $props();
|
|
14
|
-
|
|
15
|
-
let is_focused = $derived.by(() => {
|
|
16
|
-
return svedit.doc.selection?.type === 'text' && path.join('.') === svedit.doc.selection?.path.join('.');
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Converts text with annotations into renderable fragments for display.
|
|
21
|
-
* @param {string} text - The plain text content
|
|
22
|
-
* @param {Array<[number, number, string, ...any]>} annotations - Array of annotations where each is [start_offset, end_offset, type, options?] (minimum 3 elements)
|
|
23
|
-
* @returns {Array<string|AnnotationFragment>} Array of fragments - strings for plain text, AnnotationFragment objects for annotated content
|
|
24
|
-
*/
|
|
25
|
-
function render_annotated_string(text, annotations) {
|
|
26
|
-
let fragments = [];
|
|
27
|
-
let last_index = 0;
|
|
28
|
-
|
|
29
|
-
// Sort annotations by start_offset
|
|
30
|
-
const sorted_annotations = $state.snapshot(annotations).sort((a, b) => a[0] - b[0]);
|
|
31
|
-
|
|
32
|
-
for (let [index, annotation] of sorted_annotations.entries()) {
|
|
33
|
-
// Add text before the annotation
|
|
34
|
-
if (annotation[0] > last_index) {
|
|
35
|
-
fragments.push(text.slice(last_index, annotation[0]));
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Add the annotated string
|
|
39
|
-
const annotated_content = text.slice(annotation[0], annotation[1]);
|
|
40
|
-
fragments.push({
|
|
41
|
-
type: annotation[2],
|
|
42
|
-
content: annotated_content,
|
|
43
|
-
annotation_index: index,
|
|
44
|
-
data: annotation[3]
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
last_index = annotation[1];
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Add any remaining text after the last annotation
|
|
51
|
-
if (last_index < text.length) {
|
|
52
|
-
fragments.push(text.slice(last_index));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return fragments;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
let fragments = $derived(render_annotated_string(svedit.doc.get(path)[0], svedit.doc.get(path)[1]));
|
|
59
|
-
let plain_text = $derived(svedit.doc.get(path)[0]);
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* @param {MouseEvent} e - The click event
|
|
63
|
-
*/
|
|
64
|
-
function handle_link_click(e) {
|
|
65
|
-
e.preventDefault();
|
|
66
|
-
}
|
|
67
|
-
</script>
|
|
68
|
-
|
|
69
|
-
<!-- ATTENTION: The comments are needed to prevent unwanted text nodes with whitespace. -->
|
|
70
|
-
<div
|
|
71
|
-
data-type="text"
|
|
72
|
-
data-path={path.join('.')}
|
|
73
|
-
style="anchor-name: --{path.join('-')};"
|
|
74
|
-
class="text svedit-selectable {css_class}"
|
|
75
|
-
class:empty={plain_text.length === 0}
|
|
76
|
-
class:focused={is_focused}
|
|
77
|
-
placeholder={placeholder}
|
|
78
|
-
>
|
|
79
|
-
{#each fragments as fragment, index (index)}
|
|
80
|
-
{#if typeof fragment === 'string'}<!--
|
|
81
|
-
-->{fragment}<!--
|
|
82
|
-
-->{:else if fragment.type === 'emphasis'}<!--
|
|
83
|
-
--><em>{fragment.content}</em><!--
|
|
84
|
-
-->{:else if fragment.type === 'strong'}<!--
|
|
85
|
-
--><strong>{fragment.content}</strong><!--
|
|
86
|
-
-->{:else if fragment.type === 'link'}<!--
|
|
87
|
-
--><a
|
|
88
|
-
onclick={handle_link_click}
|
|
89
|
-
style="anchor-name: --{path.join('-') + '-' + fragment.annotation_index};"
|
|
90
|
-
href={fragment.data.href}
|
|
91
|
-
target={fragment.data.target || '_self'}>{fragment.content}</a><!--
|
|
92
|
-
-->{:else}<!--
|
|
93
|
-
-->{fragment.content}<!--
|
|
94
|
-
-->{/if}
|
|
95
|
-
{/each}<!--
|
|
96
|
-
--><br>
|
|
97
|
-
</div>
|
|
98
|
-
|
|
99
|
-
<style>
|
|
100
|
-
.text {
|
|
101
|
-
white-space: pre-wrap;
|
|
102
|
-
overflow-wrap: anywhere;
|
|
103
|
-
box-sizing: content-box;
|
|
104
|
-
|
|
105
|
-
&.heading1 {
|
|
106
|
-
text-wrap: var(--text-wrap);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
[placeholder].empty::before {
|
|
111
|
-
content: attr(placeholder);
|
|
112
|
-
pointer-events: none;
|
|
113
|
-
color: color-mix(in oklch, currentcolor 50%, transparent);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/* We can safely hide the <br> element when the placeholder is empty and focused. */
|
|
117
|
-
/* No longer the cursor will be rendered after the placeholder when focused. */
|
|
118
|
-
[placeholder].empty.focused br {
|
|
119
|
-
display:none;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
.text.focused {
|
|
123
|
-
background: none;
|
|
124
|
-
outline: 1px dashed var(--editing-stroke-color);
|
|
125
|
-
}
|
|
126
|
-
</style>
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export default AnnotatedStringProperty;
|
|
2
|
-
type AnnotatedStringProperty = {
|
|
3
|
-
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
-
$set?(props: Partial<AnnotatedStringPropertyProps>): void;
|
|
5
|
-
};
|
|
6
|
-
declare const AnnotatedStringProperty: import("svelte").Component<import("./types.d.ts").AnnotatedStringPropertyProps, {}, "">;
|
|
7
|
-
//# sourceMappingURL=AnnotatedStringProperty.svelte.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"AnnotatedStringProperty.svelte.d.ts","sourceRoot":"","sources":["../src/lib/AnnotatedStringProperty.svelte.js"],"names":[],"mappings":";;;;;AAoFA,+HAAsE"}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import { getContext } from 'svelte';
|
|
3
|
-
|
|
4
|
-
/** @import { AnnotatedStringPropertyProps, AnnotationFragment } from './types.d.ts'; */
|
|
5
|
-
|
|
6
|
-
const svedit = getContext('svedit');
|
|
7
|
-
|
|
8
|
-
/** @type {AnnotatedStringPropertyProps} */
|
|
9
|
-
let {
|
|
10
|
-
path,
|
|
11
|
-
class: css_class,
|
|
12
|
-
placeholder = '',
|
|
13
|
-
} = $props();
|
|
14
|
-
|
|
15
|
-
let is_focused = $derived.by(() => {
|
|
16
|
-
return svedit.doc.selection?.type === 'text' && path.join('.') === svedit.doc.selection?.path.join('.');
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Converts text with annotations into renderable fragments for display.
|
|
21
|
-
* @param {string} text - The plain text content
|
|
22
|
-
* @param {Array<[number, number, string, ...any]>} annotations - Array of annotations where each is [start_offset, end_offset, type, options?] (minimum 3 elements)
|
|
23
|
-
* @returns {Array<string|AnnotationFragment>} Array of fragments - strings for plain text, AnnotationFragment objects for annotated content
|
|
24
|
-
*/
|
|
25
|
-
function render_annotated_string(text, annotations) {
|
|
26
|
-
let fragments = [];
|
|
27
|
-
let last_index = 0;
|
|
28
|
-
|
|
29
|
-
// Sort annotations by start_offset
|
|
30
|
-
const sorted_annotations = $state.snapshot(annotations).sort((a, b) => a[0] - b[0]);
|
|
31
|
-
|
|
32
|
-
for (let [index, annotation] of sorted_annotations.entries()) {
|
|
33
|
-
// Add text before the annotation
|
|
34
|
-
if (annotation[0] > last_index) {
|
|
35
|
-
fragments.push(text.slice(last_index, annotation[0]));
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Add the annotated string
|
|
39
|
-
const annotated_content = text.slice(annotation[0], annotation[1]);
|
|
40
|
-
fragments.push({
|
|
41
|
-
type: annotation[2],
|
|
42
|
-
content: annotated_content,
|
|
43
|
-
annotation_index: index,
|
|
44
|
-
data: annotation[3]
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
last_index = annotation[1];
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Add any remaining text after the last annotation
|
|
51
|
-
if (last_index < text.length) {
|
|
52
|
-
fragments.push(text.slice(last_index));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return fragments;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
let fragments = $derived(render_annotated_string(svedit.doc.get(path)[0], svedit.doc.get(path)[1]));
|
|
59
|
-
let plain_text = $derived(svedit.doc.get(path)[0]);
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* @param {MouseEvent} e - The click event
|
|
63
|
-
*/
|
|
64
|
-
function handle_link_click(e) {
|
|
65
|
-
e.preventDefault();
|
|
66
|
-
}
|
|
67
|
-
</script>
|
|
68
|
-
|
|
69
|
-
<!-- ATTENTION: The comments are needed to prevent unwanted text nodes with whitespace. -->
|
|
70
|
-
<div
|
|
71
|
-
data-type="text"
|
|
72
|
-
data-path={path.join('.')}
|
|
73
|
-
style="anchor-name: --{path.join('-')};"
|
|
74
|
-
class="text svedit-selectable {css_class}"
|
|
75
|
-
class:empty={plain_text.length === 0}
|
|
76
|
-
class:focused={is_focused}
|
|
77
|
-
placeholder={placeholder}
|
|
78
|
-
>
|
|
79
|
-
{#each fragments as fragment, index (index)}
|
|
80
|
-
{#if typeof fragment === 'string'}<!--
|
|
81
|
-
-->{fragment}<!--
|
|
82
|
-
-->{:else if fragment.type === 'emphasis'}<!--
|
|
83
|
-
--><em>{fragment.content}</em><!--
|
|
84
|
-
-->{:else if fragment.type === 'strong'}<!--
|
|
85
|
-
--><strong>{fragment.content}</strong><!--
|
|
86
|
-
-->{:else if fragment.type === 'link'}<!--
|
|
87
|
-
--><a
|
|
88
|
-
onclick={handle_link_click}
|
|
89
|
-
style="anchor-name: --{path.join('-') + '-' + fragment.annotation_index};"
|
|
90
|
-
href={fragment.data.href}
|
|
91
|
-
target={fragment.data.target || '_self'}>{fragment.content}</a><!--
|
|
92
|
-
-->{:else}<!--
|
|
93
|
-
-->{fragment.content}<!--
|
|
94
|
-
-->{/if}
|
|
95
|
-
{/each}<!--
|
|
96
|
-
--><br>
|
|
97
|
-
</div>
|
|
98
|
-
|
|
99
|
-
<style>
|
|
100
|
-
.text {
|
|
101
|
-
white-space: pre-wrap;
|
|
102
|
-
overflow-wrap: anywhere;
|
|
103
|
-
box-sizing: content-box;
|
|
104
|
-
|
|
105
|
-
&.heading1 {
|
|
106
|
-
text-wrap: var(--text-wrap);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
[placeholder].empty::before {
|
|
111
|
-
content: attr(placeholder);
|
|
112
|
-
pointer-events: none;
|
|
113
|
-
color: color-mix(in oklch, currentcolor 50%, transparent);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/* We can safely hide the <br> element when the placeholder is empty and focused. */
|
|
117
|
-
/* No longer the cursor will be rendered after the placeholder when focused. */
|
|
118
|
-
[placeholder].empty.focused br {
|
|
119
|
-
display:none;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
.text.focused {
|
|
123
|
-
background: none;
|
|
124
|
-
outline: 1px dashed var(--editing-stroke-color);
|
|
125
|
-
}
|
|
126
|
-
</style>
|