-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrich_text_lite.js
161 lines (128 loc) · 4.55 KB
/
rich_text_lite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Extracted from https://github.com/bluesky-social/atproto/
Copyright (c) 2022-2024 Bluesky PBC, and Contributors
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// packages/api/src/rich-text/rich-text.ts
class RichTextSegment {
/** @param {string} text, @param {json} [facet] */
constructor(text, facet) {
this.text = text;
this.facet = facet;
}
/** @returns {object | undefined} */
get link() {
return this.facet?.features?.find(v => v.$type === 'app.bsky.richtext.facet#link');
}
/** @returns {object | undefined} */
get mention() {
return this.facet?.features?.find(v => v.$type === 'app.bsky.richtext.facet#mention');
}
/** @returns {object | undefined} */
get tag() {
return this.facet?.features?.find(v => v.$type === 'app.bsky.richtext.facet#tag');
}
}
class RichText {
/** @params {json} props */
constructor(props) {
this.unicodeText = new UnicodeString(props.text);
this.facets = props.facets;
if (this.facets) {
this.facets.sort((a, b) => a.index.byteStart - b.index.byteStart);
}
}
/** @returns {string} */
get text() {
return this.unicodeText.toString();
}
/** @returns {number} */
get length() {
return this.unicodeText.length;
}
/** @returns {number} */
get graphemeLength() {
return this.unicodeText.graphemeLength;
}
*segments() {
const facets = this.facets || [];
if (facets.length == 0) {
yield new RichTextSegment(this.unicodeText.utf16);
return;
}
let textCursor = 0;
let facetCursor = 0;
do {
const currFacet = facets[facetCursor];
if (textCursor < currFacet.index.byteStart) {
yield new RichTextSegment(this.unicodeText.slice(textCursor, currFacet.index.byteStart));
} else if (textCursor > currFacet.index.byteStart) {
facetCursor++;
continue;
}
if (currFacet.index.byteStart < currFacet.index.byteEnd) {
const subtext = this.unicodeText.slice(currFacet.index.byteStart, currFacet.index.byteEnd);
if (subtext.trim().length == 0) {
yield new RichTextSegment(subtext);
} else {
yield new RichTextSegment(subtext, currFacet);
}
}
textCursor = currFacet.index.byteEnd;
facetCursor++;
} while (facetCursor < facets.length);
if (textCursor < this.unicodeText.length) {
yield new RichTextSegment(this.unicodeText.slice(textCursor, this.unicodeText.length));
}
}
}
// packages/api/src/rich-text/unicode.ts
/**
* Javascript uses utf16-encoded strings while most environments and specs
* have standardized around utf8 (including JSON).
*
* After some lengthy debated we decided that richtext facets need to use
* utf8 indices. This means we need tools to convert indices between utf8
* and utf16, and that's precisely what this library handles.
*/
class UnicodeString {
static encoder = new TextEncoder();
static decoder = new TextDecoder();
static segmenter = window.Intl && Intl.Segmenter && new Intl.Segmenter();
/** @param {string} utf16 */
constructor(utf16) {
this.utf16 = utf16;
this.utf8 = UnicodeString.encoder.encode(utf16);
}
/** @returns number */
get length() {
return this.utf8.byteLength;
}
/** @returns number */
get graphemeLength() {
return Array.from(UnicodeString.segmenter.segment(this.utf16)).length;
}
/** @param {number} start, @param {number} end, @returns string */
slice(start, end) {
return UnicodeString.decoder.decode(this.utf8.slice(start, end));
}
/** @returns string */
toString() {
return this.utf16;
}
}