svedit 0.6.3 → 0.7.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 +117 -45
- package/dist/AnnotatedTextProperty.svelte +19 -16
- package/dist/AnnotatedTextProperty.svelte.d.ts.map +1 -1
- package/dist/Command.svelte.d.ts +3 -3
- package/dist/Command.svelte.js +5 -5
- package/dist/CustomProperty.svelte +5 -4
- package/dist/Node.svelte +29 -14
- package/dist/Node.svelte.d.ts.map +1 -1
- package/dist/NodeArrayProperty.svelte +29 -9
- package/dist/NodeArrayProperty.svelte.d.ts.map +1 -1
- package/dist/NodeCaret.svelte +71 -0
- package/dist/NodeCaret.svelte.d.ts +27 -0
- package/dist/NodeCaret.svelte.d.ts.map +1 -0
- package/dist/NodeGap.svelte +249 -0
- package/dist/NodeGap.svelte.d.ts +20 -0
- package/dist/NodeGap.svelte.d.ts.map +1 -0
- package/dist/NodeGapMarkers.svelte +456 -0
- package/dist/NodeGapMarkers.svelte.d.ts +12 -0
- package/dist/NodeGapMarkers.svelte.d.ts.map +1 -0
- package/dist/NodeSelectionMarkers.svelte +49 -0
- package/dist/NodeSelectionMarkers.svelte.d.ts +7 -0
- package/dist/NodeSelectionMarkers.svelte.d.ts.map +1 -0
- package/dist/Session.svelte.d.ts +6 -6
- package/dist/Session.svelte.d.ts.map +1 -1
- package/dist/Session.svelte.js +13 -69
- package/dist/Svedit.svelte +158 -90
- package/dist/Svedit.svelte.d.ts.map +1 -1
- package/dist/Transaction.svelte.d.ts +1 -1
- package/dist/Transaction.svelte.d.ts.map +1 -1
- package/dist/Transaction.svelte.js +10 -70
- package/dist/doc_utils.d.ts +12 -0
- package/dist/doc_utils.d.ts.map +1 -1
- package/dist/doc_utils.js +61 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/node_gap_computation.svelte.d.ts +5 -0
- package/dist/node_gap_computation.svelte.d.ts.map +1 -0
- package/dist/node_gap_computation.svelte.js +621 -0
- package/dist/styles/svedit-colors.css +8 -0
- package/dist/transforms.svelte.d.ts.map +1 -1
- package/dist/transforms.svelte.js +7 -10
- package/dist/types.d.ts +56 -68
- package/dist/utils.d.ts +0 -3
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +10 -10
- package/package.json +21 -21
- package/src/lib/AnnotatedTextProperty.svelte +19 -16
- package/src/lib/Command.svelte.js +5 -5
- package/src/lib/CustomProperty.svelte +5 -4
- package/src/lib/Node.svelte +29 -14
- package/src/lib/NodeArrayProperty.svelte +29 -9
- package/src/lib/NodeCaret.svelte +71 -0
- package/src/lib/NodeGap.svelte +249 -0
- package/src/lib/NodeGapMarkers.svelte +456 -0
- package/src/lib/NodeSelectionMarkers.svelte +49 -0
- package/src/lib/Session.svelte.js +13 -69
- package/src/lib/Svedit.svelte +158 -90
- package/src/lib/Transaction.svelte.js +10 -70
- package/src/lib/doc_utils.js +61 -2
- package/src/lib/index.js +4 -0
- package/src/lib/node_gap_computation.svelte.js +621 -0
- package/src/lib/styles/svedit-colors.css +8 -0
- package/src/lib/transforms.svelte.js +7 -10
- package/src/lib/types.d.ts +56 -68
- package/src/lib/utils.js +10 -10
package/src/lib/types.d.ts
CHANGED
|
@@ -93,59 +93,53 @@ export type PrimitiveType = ScalarType | ArrayType | RichType;
|
|
|
93
93
|
*/
|
|
94
94
|
export type PropertyType = PrimitiveType | ReferenceType;
|
|
95
95
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
* Converts a document node schema definition to its inferred JS type.
|
|
144
|
-
* Handles the {type: "..."} wrapper structure used in document schemas.
|
|
145
|
-
*/
|
|
146
|
-
export type DocumentNodeToJs<S extends NodeSchema> = { id: string; type: string } & {
|
|
147
|
-
[K in keyof S['properties']]: DocumentSchemaValueToJs<S['properties'][K]['type']>;
|
|
148
|
-
};
|
|
96
|
+
// TODO: We may later want more sophisticated type inference, so that typing
|
|
97
|
+
// e.g. `session.get('image')` auto-completes the node's properties based on
|
|
98
|
+
// the schema definition. The types below are a starting point for that.
|
|
99
|
+
//
|
|
100
|
+
// export type DocumentSchemaPrimitive =
|
|
101
|
+
// | 'string'
|
|
102
|
+
// | 'number'
|
|
103
|
+
// | 'boolean'
|
|
104
|
+
// | 'integer'
|
|
105
|
+
// | 'datetime'
|
|
106
|
+
// | 'string_array'
|
|
107
|
+
// | 'number_array'
|
|
108
|
+
// | 'boolean_array'
|
|
109
|
+
// | 'integer_array'
|
|
110
|
+
// | 'annotated_text'
|
|
111
|
+
// | 'node'
|
|
112
|
+
// | 'node_array';
|
|
113
|
+
//
|
|
114
|
+
// export type DocumentSchemaValueToJs<T> = T extends 'string'
|
|
115
|
+
// ? string
|
|
116
|
+
// : T extends 'number'
|
|
117
|
+
// ? number
|
|
118
|
+
// : T extends 'boolean'
|
|
119
|
+
// ? boolean
|
|
120
|
+
// : T extends 'integer'
|
|
121
|
+
// ? number
|
|
122
|
+
// : T extends 'datetime'
|
|
123
|
+
// ? string
|
|
124
|
+
// : T extends 'string_array'
|
|
125
|
+
// ? Array<string>
|
|
126
|
+
// : T extends 'number_array'
|
|
127
|
+
// ? Array<number>
|
|
128
|
+
// : T extends 'boolean_array'
|
|
129
|
+
// ? Array<boolean>
|
|
130
|
+
// : T extends 'integer_array'
|
|
131
|
+
// ? Array<number>
|
|
132
|
+
// : T extends 'annotated_text'
|
|
133
|
+
// ? AnnotatedText
|
|
134
|
+
// : T extends 'node'
|
|
135
|
+
// ? string
|
|
136
|
+
// : T extends 'node_array'
|
|
137
|
+
// ? Array<string>
|
|
138
|
+
// : never;
|
|
139
|
+
//
|
|
140
|
+
// export type DocumentNodeToJs<S extends NodeSchema> = { id: string; type: string } & {
|
|
141
|
+
// [K in keyof S['properties']]: DocumentSchemaValueToJs<S['properties'][K]['type']>;
|
|
142
|
+
// };
|
|
149
143
|
|
|
150
144
|
/**
|
|
151
145
|
* A property that stores an annotated text with required allow_newlines setting.
|
|
@@ -337,6 +331,8 @@ export type AnnotatedTextPropertyProps = {
|
|
|
337
331
|
class?: string;
|
|
338
332
|
/** A placeholder to be rendered for empty content */
|
|
339
333
|
placeholder?: string;
|
|
334
|
+
/** Rest props to spread onto the rendered element (e.g. href, target, etc.) */
|
|
335
|
+
[key: string]: any;
|
|
340
336
|
};
|
|
341
337
|
|
|
342
338
|
/**
|
|
@@ -351,6 +347,8 @@ export type CustomPropertyProps = {
|
|
|
351
347
|
class?: string;
|
|
352
348
|
/** The content of the custom property (e.g. an image) */
|
|
353
349
|
children: Snippet;
|
|
350
|
+
/** Rest props to spread onto the rendered element */
|
|
351
|
+
[key: string]: any;
|
|
354
352
|
};
|
|
355
353
|
|
|
356
354
|
/**
|
|
@@ -361,10 +359,10 @@ export type NodeArrayPropertyProps = {
|
|
|
361
359
|
path: DocumentPath;
|
|
362
360
|
/** Optional custom HTML tag */
|
|
363
361
|
tag?: string;
|
|
364
|
-
/** Optional custom HTML tag */
|
|
365
|
-
tag?: string;
|
|
366
362
|
/** The `class` attribute on the container element */
|
|
367
363
|
class?: string;
|
|
364
|
+
/** Rest props to spread onto the rendered element (e.g. href, target, etc.) */
|
|
365
|
+
[key: string]: any;
|
|
368
366
|
};
|
|
369
367
|
|
|
370
368
|
/**
|
|
@@ -379,22 +377,12 @@ export type NodeProps = {
|
|
|
379
377
|
class?: string;
|
|
380
378
|
/** The type-specific content of the node */
|
|
381
379
|
children: Snippet;
|
|
380
|
+
/** Rest props to spread onto the rendered element (e.g. href, target, etc.) */
|
|
381
|
+
[key: string]: any;
|
|
382
382
|
};
|
|
383
383
|
|
|
384
384
|
/**
|
|
385
|
-
* Props for the
|
|
386
|
-
*/
|
|
387
|
-
export type CustomPropertyProps = {
|
|
388
|
-
/** The full path to the property */
|
|
389
|
-
path: DocumentPath;
|
|
390
|
-
/** The `class` attribute on the content element */
|
|
391
|
-
class?: string;
|
|
392
|
-
/** The content of the custom property (e.g. an image) */
|
|
393
|
-
children: Snippet;
|
|
394
|
-
};
|
|
395
|
-
|
|
396
|
-
/**
|
|
397
|
-
* Props for the CustomProperty component
|
|
385
|
+
* Props for the Svedit component
|
|
398
386
|
*/
|
|
399
387
|
export type SveditProps = {
|
|
400
388
|
/** The session instance */
|
package/src/lib/utils.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
* @import { Annotation, AnnotatedText, SelectionRange, Selection } from './types.d.ts';
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
const SEGMENTER = new Intl.Segmenter('en', { granularity: 'grapheme' });
|
|
6
|
+
|
|
5
7
|
/**
|
|
6
8
|
* Detect if the current browser is on a mobile device
|
|
7
9
|
* @returns {boolean} true if mobile browser, false otherwise
|
|
@@ -19,6 +21,7 @@ export function is_mobile_browser() {
|
|
|
19
21
|
);
|
|
20
22
|
}
|
|
21
23
|
|
|
24
|
+
// ‼️‼️‼️‼️‼️‼️ UNUSED UTILITY BELOW ‼️‼️‼️‼️‼️‼️
|
|
22
25
|
/**
|
|
23
26
|
* Detect if the current browser is Chrome on desktop
|
|
24
27
|
* @returns {boolean} true if Chrome desktop browser, false otherwise
|
|
@@ -48,10 +51,11 @@ export function is_chrome_desktop_browser() {
|
|
|
48
51
|
* get_char_length('👋🏽') // Returns: 1 (not 4 - skin tone modifier treated as single char)
|
|
49
52
|
*/
|
|
50
53
|
export function get_char_length(str) {
|
|
51
|
-
|
|
52
|
-
return [...segmenter.segment(str)].length;
|
|
54
|
+
return [...SEGMENTER.segment(str)].length;
|
|
53
55
|
}
|
|
54
56
|
|
|
57
|
+
|
|
58
|
+
// ‼️‼️‼️‼️‼️‼️ UNUSED UTILITY BELOW ‼️‼️‼️‼️‼️‼️
|
|
55
59
|
/**
|
|
56
60
|
* Get a single character at the specified position (accounting for multi-byte characters)
|
|
57
61
|
*
|
|
@@ -68,8 +72,7 @@ export function get_char_length(str) {
|
|
|
68
72
|
* get_char_at('👋🏽', 0) // Returns: '👋🏽' (skin tone modifier included)
|
|
69
73
|
*/
|
|
70
74
|
export function get_char_at(str, index) {
|
|
71
|
-
const
|
|
72
|
-
const segments = [...segmenter.segment(str)];
|
|
75
|
+
const segments = [...SEGMENTER.segment(str)];
|
|
73
76
|
return segments[index].segment;
|
|
74
77
|
}
|
|
75
78
|
|
|
@@ -89,8 +92,7 @@ export function get_char_at(str, index) {
|
|
|
89
92
|
* char_slice('a👋🏽b', 1, 2) // Returns: '👋🏽' (skin tone modifier included)
|
|
90
93
|
*/
|
|
91
94
|
export function char_slice(str, start, end = undefined) {
|
|
92
|
-
const
|
|
93
|
-
const segments = [...segmenter.segment(str)];
|
|
95
|
+
const segments = [...SEGMENTER.segment(str)];
|
|
94
96
|
return segments
|
|
95
97
|
.slice(start, end)
|
|
96
98
|
.map((s) => s.segment)
|
|
@@ -112,8 +114,7 @@ export function char_slice(str, start, end = undefined) {
|
|
|
112
114
|
* utf16_to_char_offset("a😀b", 3) // Returns: 2 (position after emoji)
|
|
113
115
|
*/
|
|
114
116
|
export function utf16_to_char_offset(str, utf16_offset) {
|
|
115
|
-
const
|
|
116
|
-
const segments = [...segmenter.segment(str)];
|
|
117
|
+
const segments = [...SEGMENTER.segment(str)];
|
|
117
118
|
let char_offset = 0;
|
|
118
119
|
let utf16_count = 0;
|
|
119
120
|
|
|
@@ -142,8 +143,7 @@ export function utf16_to_char_offset(str, utf16_offset) {
|
|
|
142
143
|
* char_to_utf16_offset("a😀b", 2) // Returns: 3 (UTF-16 position after emoji)
|
|
143
144
|
*/
|
|
144
145
|
export function char_to_utf16_offset(str, char_offset) {
|
|
145
|
-
const
|
|
146
|
-
const segments = [...segmenter.segment(str)];
|
|
146
|
+
const segments = [...SEGMENTER.segment(str)];
|
|
147
147
|
let utf16_offset = 0;
|
|
148
148
|
|
|
149
149
|
for (let i = 0; i < Math.min(char_offset, segments.length); i++) {
|