foldkit-richtext 0.5.3 → 0.6.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.
Files changed (100) hide show
  1. package/dist/clipboard.d.ts +224 -0
  2. package/dist/clipboard.d.ts.map +1 -0
  3. package/dist/clipboard.js +208 -0
  4. package/dist/clipboard.js.map +1 -0
  5. package/dist/code.d.ts +27 -0
  6. package/dist/code.d.ts.map +1 -0
  7. package/dist/code.js +45 -0
  8. package/dist/code.js.map +1 -0
  9. package/dist/command.d.ts +300 -0
  10. package/dist/command.d.ts.map +1 -0
  11. package/dist/command.js +1438 -0
  12. package/dist/command.js.map +1 -0
  13. package/dist/decoration.d.ts +44 -0
  14. package/dist/decoration.d.ts.map +1 -0
  15. package/dist/decoration.js +96 -0
  16. package/dist/decoration.js.map +1 -0
  17. package/dist/document.d.ts +1045 -0
  18. package/dist/document.d.ts.map +1 -0
  19. package/dist/document.js +544 -0
  20. package/dist/document.js.map +1 -0
  21. package/dist/footnote.d.ts +35 -0
  22. package/dist/footnote.d.ts.map +1 -0
  23. package/dist/footnote.js +100 -0
  24. package/dist/footnote.js.map +1 -0
  25. package/dist/history.d.ts +470 -0
  26. package/dist/history.d.ts.map +1 -0
  27. package/dist/history.js +81 -0
  28. package/dist/history.js.map +1 -0
  29. package/dist/html.d.ts +27 -0
  30. package/dist/html.d.ts.map +1 -0
  31. package/dist/html.js +99 -0
  32. package/dist/html.js.map +1 -0
  33. package/dist/indent.d.ts +16 -0
  34. package/dist/indent.d.ts.map +1 -0
  35. package/dist/indent.js +101 -0
  36. package/dist/indent.js.map +1 -0
  37. package/dist/index.d.ts +24 -0
  38. package/dist/index.js +24 -0
  39. package/dist/input.d.ts +54 -0
  40. package/dist/input.d.ts.map +1 -0
  41. package/dist/input.js +37 -0
  42. package/dist/input.js.map +1 -0
  43. package/dist/kit.d.ts +179 -0
  44. package/dist/kit.d.ts.map +1 -0
  45. package/dist/kit.js +202 -0
  46. package/dist/kit.js.map +1 -0
  47. package/dist/marks.d.ts +79 -0
  48. package/dist/marks.d.ts.map +1 -0
  49. package/dist/marks.js +127 -0
  50. package/dist/marks.js.map +1 -0
  51. package/dist/media.d.ts +30 -0
  52. package/dist/media.d.ts.map +1 -0
  53. package/dist/media.js +148 -0
  54. package/dist/media.js.map +1 -0
  55. package/dist/migration.d.ts +55 -0
  56. package/dist/migration.d.ts.map +1 -0
  57. package/dist/migration.js +124 -0
  58. package/dist/migration.js.map +1 -0
  59. package/dist/outline.d.ts +15 -0
  60. package/dist/outline.d.ts.map +1 -0
  61. package/dist/outline.js +39 -0
  62. package/dist/outline.js.map +1 -0
  63. package/dist/rendering.d.ts +96 -0
  64. package/dist/rendering.d.ts.map +1 -0
  65. package/dist/rendering.js +106 -0
  66. package/dist/rendering.js.map +1 -0
  67. package/dist/replicated.d.ts +340 -0
  68. package/dist/replicated.d.ts.map +1 -0
  69. package/dist/replicated.js +1727 -0
  70. package/dist/replicated.js.map +1 -0
  71. package/dist/search.d.ts +28 -0
  72. package/dist/search.d.ts.map +1 -0
  73. package/dist/search.js +61 -0
  74. package/dist/search.js.map +1 -0
  75. package/dist/standard.d.ts +61 -0
  76. package/dist/standard.d.ts.map +1 -0
  77. package/dist/standard.js +231 -0
  78. package/dist/standard.js.map +1 -0
  79. package/dist/table.d.ts +38 -0
  80. package/dist/table.d.ts.map +1 -0
  81. package/dist/table.js +160 -0
  82. package/dist/table.js.map +1 -0
  83. package/dist/transaction.d.ts +712 -0
  84. package/dist/transaction.d.ts.map +1 -0
  85. package/dist/transaction.js +993 -0
  86. package/dist/transaction.js.map +1 -0
  87. package/dist/transform.d.ts +48 -0
  88. package/dist/transform.d.ts.map +1 -0
  89. package/dist/transform.js +94 -0
  90. package/dist/transform.js.map +1 -0
  91. package/dist/url.d.ts +16 -0
  92. package/dist/url.d.ts.map +1 -0
  93. package/dist/url.js +32 -0
  94. package/dist/url.js.map +1 -0
  95. package/package.json +6 -6
  96. package/dist/index.d.mts +0 -3787
  97. package/dist/index.d.mts.map +0 -1
  98. package/dist/index.mjs +0 -6688
  99. package/dist/index.mjs.map +0 -1
  100. package/dist/rolldown-runtime-D8jQpDG7.mjs +0 -12
@@ -0,0 +1,224 @@
1
+ import { Schema } from "effect";
2
+ import { Block, Document, Selection } from "./document.js";
3
+ /**
4
+ * Clipboard content is semantic, not HTML (§69). A slice is a versioned,
5
+ * identity-carrying fragment of a document: it round-trips through an
6
+ * application's own format, and a paste mints fresh identities so nothing
7
+ * collides with the document it lands in. HTML and plain text are interchange
8
+ * fallbacks the adapter adds; they are not the slice.
9
+ */
10
+ export declare const Slice: Schema.Struct<{
11
+ readonly version: Schema.Literal<1>;
12
+ readonly blocks: Schema.$Array<Schema.Union<readonly [Schema.Struct<{
13
+ readonly type: Schema.Literal<"Paragraph">;
14
+ readonly id: Schema.brand<Schema.NonEmptyString, "foldkit-richtext/NodeId">;
15
+ readonly children: Schema.$Array<Schema.Struct<{
16
+ readonly type: Schema.Literal<"Text">;
17
+ readonly id: Schema.brand<Schema.NonEmptyString, "foldkit-richtext/NodeId">;
18
+ readonly text: Schema.String;
19
+ readonly marks: Schema.$Array<Schema.Union<readonly [Schema.NonEmptyString, Schema.Struct<{
20
+ readonly name: Schema.NonEmptyString;
21
+ readonly props: Schema.optionalKey<Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>>;
22
+ }>]>>;
23
+ }>>;
24
+ }>, Schema.Struct<{
25
+ readonly type: Schema.Literal<"Heading">;
26
+ readonly id: Schema.brand<Schema.NonEmptyString, "foldkit-richtext/NodeId">;
27
+ readonly level: Schema.Literals<readonly [1, 2, 3, 4, 5, 6]>;
28
+ readonly children: Schema.$Array<Schema.Struct<{
29
+ readonly type: Schema.Literal<"Text">;
30
+ readonly id: Schema.brand<Schema.NonEmptyString, "foldkit-richtext/NodeId">;
31
+ readonly text: Schema.String;
32
+ readonly marks: Schema.$Array<Schema.Union<readonly [Schema.NonEmptyString, Schema.Struct<{
33
+ readonly name: Schema.NonEmptyString;
34
+ readonly props: Schema.optionalKey<Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>>;
35
+ }>]>>;
36
+ }>>;
37
+ }>, Schema.Struct<{
38
+ readonly type: Schema.Literal<"Node">;
39
+ readonly kind: Schema.NonEmptyString;
40
+ readonly id: Schema.brand<Schema.NonEmptyString, "foldkit-richtext/NodeId">;
41
+ readonly props: Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>;
42
+ readonly children: Schema.$Array<Schema.Struct<{
43
+ readonly type: Schema.Literal<"Text">;
44
+ readonly id: Schema.brand<Schema.NonEmptyString, "foldkit-richtext/NodeId">;
45
+ readonly text: Schema.String;
46
+ readonly marks: Schema.$Array<Schema.Union<readonly [Schema.NonEmptyString, Schema.Struct<{
47
+ readonly name: Schema.NonEmptyString;
48
+ readonly props: Schema.optionalKey<Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>>;
49
+ }>]>>;
50
+ }>>;
51
+ readonly blocks: Schema.optionalKey<Schema.suspend<Schema.Codec<readonly Block[], readonly ({
52
+ readonly type: 'Node';
53
+ readonly kind: string;
54
+ readonly id: string;
55
+ readonly props: {
56
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
57
+ };
58
+ readonly children: readonly {
59
+ readonly type: "Text";
60
+ readonly id: string;
61
+ readonly text: string;
62
+ readonly marks: readonly (string | {
63
+ readonly name: string;
64
+ readonly props?: {
65
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | {
66
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null;
67
+ } | null;
68
+ };
69
+ })[];
70
+ }[];
71
+ readonly blocks?: readonly ( /*elided*/ any | {
72
+ readonly type: "Paragraph";
73
+ readonly id: string;
74
+ readonly children: readonly {
75
+ readonly type: "Text";
76
+ readonly id: string;
77
+ readonly text: string;
78
+ readonly marks: readonly (string | {
79
+ readonly name: string;
80
+ readonly props?: {
81
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | {
82
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null;
83
+ } | null;
84
+ };
85
+ })[];
86
+ }[];
87
+ } | {
88
+ readonly type: "Heading";
89
+ readonly id: string;
90
+ readonly level: 1 | 2 | 3 | 4 | 5 | 6;
91
+ readonly children: readonly {
92
+ readonly type: "Text";
93
+ readonly id: string;
94
+ readonly text: string;
95
+ readonly marks: readonly (string | {
96
+ readonly name: string;
97
+ readonly props?: {
98
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | {
99
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null;
100
+ } | null;
101
+ };
102
+ })[];
103
+ }[];
104
+ } | {
105
+ readonly type: "Unknown";
106
+ readonly id: string;
107
+ readonly originalType: string;
108
+ readonly props: {
109
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | {
110
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null;
111
+ } | null;
112
+ };
113
+ readonly children: readonly {
114
+ readonly type: "Text";
115
+ readonly id: string;
116
+ readonly text: string;
117
+ readonly marks: readonly (string | {
118
+ readonly name: string;
119
+ readonly props?: {
120
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | {
121
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null;
122
+ } | null;
123
+ };
124
+ })[];
125
+ }[];
126
+ })[];
127
+ } | {
128
+ readonly type: "Paragraph";
129
+ readonly id: string;
130
+ readonly children: readonly {
131
+ readonly type: "Text";
132
+ readonly id: string;
133
+ readonly text: string;
134
+ readonly marks: readonly (string | {
135
+ readonly name: string;
136
+ readonly props?: {
137
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | {
138
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null;
139
+ } | null;
140
+ };
141
+ })[];
142
+ }[];
143
+ } | {
144
+ readonly type: "Heading";
145
+ readonly id: string;
146
+ readonly level: 1 | 2 | 3 | 4 | 5 | 6;
147
+ readonly children: readonly {
148
+ readonly type: "Text";
149
+ readonly id: string;
150
+ readonly text: string;
151
+ readonly marks: readonly (string | {
152
+ readonly name: string;
153
+ readonly props?: {
154
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | {
155
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null;
156
+ } | null;
157
+ };
158
+ })[];
159
+ }[];
160
+ } | {
161
+ readonly type: "Unknown";
162
+ readonly id: string;
163
+ readonly originalType: string;
164
+ readonly props: {
165
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | {
166
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null;
167
+ } | null;
168
+ };
169
+ readonly children: readonly {
170
+ readonly type: "Text";
171
+ readonly id: string;
172
+ readonly text: string;
173
+ readonly marks: readonly (string | {
174
+ readonly name: string;
175
+ readonly props?: {
176
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | {
177
+ readonly [x: string]: string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | readonly (string | number | boolean | any | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null)[] | any | null;
178
+ } | null;
179
+ };
180
+ })[];
181
+ }[];
182
+ })[], never, never>>>;
183
+ }>, Schema.Struct<{
184
+ readonly type: Schema.Literal<"Unknown">;
185
+ readonly id: Schema.brand<Schema.NonEmptyString, "foldkit-richtext/NodeId">;
186
+ readonly originalType: Schema.NonEmptyString;
187
+ readonly props: Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>;
188
+ readonly children: Schema.$Array<Schema.Struct<{
189
+ readonly type: Schema.Literal<"Text">;
190
+ readonly id: Schema.brand<Schema.NonEmptyString, "foldkit-richtext/NodeId">;
191
+ readonly text: Schema.String;
192
+ readonly marks: Schema.$Array<Schema.Union<readonly [Schema.NonEmptyString, Schema.Struct<{
193
+ readonly name: Schema.NonEmptyString;
194
+ readonly props: Schema.optionalKey<Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>>;
195
+ }>]>>;
196
+ }>>;
197
+ }>]>>;
198
+ }>;
199
+ export type Slice = typeof Slice.Type;
200
+ export declare const emptySlice: Slice;
201
+ /**
202
+ * The semantic content a copy would take: a whole block for a node selection,
203
+ * and for a range only the covered part of what it reaches, with runs trimmed to
204
+ * the selection. Blocks the range never enters are left out, so a partial copy
205
+ * never drags a paragraph along.
206
+ */
207
+ export declare const sliceOf: (document: Document, selection: Selection | null) => Slice | undefined;
208
+ /**
209
+ * Plain text of a slice, one line per text block. A slice and a block list have
210
+ * the same shape, so the serializer's walk serves both.
211
+ */
212
+ export declare const plainTextOf: (slice: Slice) => string;
213
+ /**
214
+ * A slice from pasted plain text: one paragraph per line. Identities come from
215
+ * the caller, like every other identity a live edit mints.
216
+ */
217
+ export declare const sliceFromText: (text: string, mint: () => string) => Slice;
218
+ /** The same content under fresh identities, so a paste can never collide. */
219
+ export declare const withFreshIds: (slice: Slice, mint: () => string) => Slice;
220
+ /** Encodes a slice for the clipboard. */
221
+ export declare const serializeSlice: (slice: Slice) => string;
222
+ /** Decodes a slice, or undefined when the payload is not one this version wrote. */
223
+ export declare const deserializeSlice: (input: unknown) => Slice | undefined;
224
+ //# sourceMappingURL=clipboard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clipboard.d.ts","names":[],"sources":["../src/clipboard.ts"],"mappings":";;;;;;;;;qBA0Ba,OAAK,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;YAgBN,eAAe,MAAM;qBAEpB,YAAY;;;;;;;qBAqFZ,UAAO,UAAc,UAAQ,WAAa,qBAAmB;;;;;qBA+B7D,cAAW,OAAW;;;;;qBAMtB,gBAAa,cAAgB,uBAAuB;;qBAiBpD,eAAY,OAAW,OAAK,uBAAuB;;qBAcnD,iBAAc,OAAW;;qBAGzB,mBAAgB,mBAAqB"}
@@ -0,0 +1,208 @@
1
+ import { Schema } from "effect";
2
+ import { toText } from "./html.js";
3
+ import { Block, NodeId, blockAtPath, comparePositionPlaces, compareRunPlaces, eachBlock, locateBlock, locateRun, pathKey } from "./document.js";
4
+ // src/clipboard.ts
5
+ /**
6
+ * Clipboard content is semantic, not HTML (§69). A slice is a versioned,
7
+ * identity-carrying fragment of a document: it round-trips through an
8
+ * application's own format, and a paste mints fresh identities so nothing
9
+ * collides with the document it lands in. HTML and plain text are interchange
10
+ * fallbacks the adapter adds; they are not the slice.
11
+ */
12
+ const Slice = Schema.Struct({
13
+ version: Schema.Literal(1),
14
+ blocks: Schema.Array(Block)
15
+ }).check(Schema.makeFilter((slice) => {
16
+ const ids = /* @__PURE__ */ new Set();
17
+ let duplicate = false;
18
+ eachBlock(slice.blocks, (block) => {
19
+ for (const id of [block.id, ...block.children.map((run) => run.id)]) {
20
+ if (ids.has(id)) duplicate = true;
21
+ ids.add(id);
22
+ }
23
+ });
24
+ return !duplicate || "Duplicate slice identity";
25
+ }));
26
+ const emptySlice = Object.freeze({
27
+ version: 1,
28
+ blocks: []
29
+ });
30
+ const decodeSlice = Schema.decodeUnknownSync(Slice, { onExcessProperty: "error" });
31
+ const encodeSlice = Schema.encodeSync(Slice);
32
+ const trim = (run, from, to) => ({
33
+ ...run,
34
+ text: run.text.slice(from, to)
35
+ });
36
+ /** Whether a block's subtree contains an endpoint, or sits between the two. */
37
+ const inRange = (blockPath, span) => {
38
+ const contains = (place) => blockPath.length <= place.path.length && blockPath.every((index, depth) => place.path[depth] === index);
39
+ if (contains(span.start) || contains(span.end)) return true;
40
+ const first = {
41
+ path: blockPath,
42
+ index: 0
43
+ };
44
+ return compareRunPlaces(first, span.start) > 0 && compareRunPlaces(first, span.end) < 0;
45
+ };
46
+ /** The runs of one block that the span covers, trimmed to it. */
47
+ const coveredRuns = (block, blockPath, span) => {
48
+ const atStart = pathKey(blockPath) === pathKey(span.start.path);
49
+ const atEnd = pathKey(blockPath) === pathKey(span.end.path);
50
+ if (!atStart && !atEnd) return block.children;
51
+ const from = atStart ? span.start.index : 0;
52
+ const to = atEnd ? span.end.index : block.children.length - 1;
53
+ const children = [];
54
+ for (const [index, run] of block.children.entries()) {
55
+ if (index < from || index > to) continue;
56
+ const trimFrom = index === from && atStart ? span.start.offset : 0;
57
+ const trimTo = index === to && atEnd ? span.end.offset : run.text.length;
58
+ if (trimFrom >= trimTo) continue;
59
+ children.push(trim(run, trimFrom, trimTo));
60
+ }
61
+ return children;
62
+ };
63
+ /**
64
+ * The covered part of a block list: the blocks the span reaches, each trimmed to
65
+ * it. A container is kept with the children the span reaches, so copying across
66
+ * two list items carries the list rather than two loose paragraphs.
67
+ */
68
+ const coveredBlocks = (blocks, prefix, span) => {
69
+ const covered = [];
70
+ for (const [index, block] of blocks.entries()) {
71
+ const path = [...prefix, index];
72
+ if (!inRange(path, span)) continue;
73
+ if (block.type === "Unknown") {
74
+ covered.push(block);
75
+ continue;
76
+ }
77
+ const children = coveredRuns(block, path, span);
78
+ if (block.type === "Node" && block.blocks !== void 0) {
79
+ const nested = coveredBlocks(block.blocks, path, span);
80
+ if (children.length === 0 && nested.length === 0) continue;
81
+ covered.push({
82
+ ...block,
83
+ children,
84
+ blocks: nested
85
+ });
86
+ continue;
87
+ }
88
+ if (children.length === 0) continue;
89
+ covered.push({
90
+ ...block,
91
+ children
92
+ });
93
+ }
94
+ return covered;
95
+ };
96
+ /**
97
+ * The semantic content a copy would take: a whole block for a node selection,
98
+ * and for a range only the covered part of what it reaches, with runs trimmed to
99
+ * the selection. Blocks the range never enters are left out, so a partial copy
100
+ * never drags a paragraph along.
101
+ */
102
+ const sliceOf = (document, selection) => {
103
+ if (selection === null) return void 0;
104
+ if (selection.type === "Node") {
105
+ const located = locateBlock(document, selection.node);
106
+ return located === void 0 ? void 0 : {
107
+ version: 1,
108
+ blocks: [located.block]
109
+ };
110
+ }
111
+ const anchor = locateRun(document, selection.anchor.node);
112
+ const focus = locateRun(document, selection.focus.node);
113
+ if (anchor === void 0 || focus === void 0) return void 0;
114
+ const anchorPlace = {
115
+ ...anchor,
116
+ offset: selection.anchor.offset
117
+ };
118
+ const focusPlace = {
119
+ ...focus,
120
+ offset: selection.focus.offset
121
+ };
122
+ const span = comparePositionPlaces(anchorPlace, focusPlace) <= 0 ? {
123
+ start: anchorPlace,
124
+ end: focusPlace
125
+ } : {
126
+ start: focusPlace,
127
+ end: anchorPlace
128
+ };
129
+ if (pathKey(span.start.path) === pathKey(span.end.path)) {
130
+ const block = blockAtPath(document, span.start.path);
131
+ if (block === void 0 || block.type === "Unknown") return {
132
+ version: 1,
133
+ blocks: []
134
+ };
135
+ const children = coveredRuns(block, span.start.path, span);
136
+ return {
137
+ version: 1,
138
+ blocks: children.length === 0 ? [] : [{
139
+ ...block,
140
+ children
141
+ }]
142
+ };
143
+ }
144
+ return {
145
+ version: 1,
146
+ blocks: coveredBlocks(document.children, [], span)
147
+ };
148
+ };
149
+ /**
150
+ * Plain text of a slice, one line per text block. A slice and a block list have
151
+ * the same shape, so the serializer's walk serves both.
152
+ */
153
+ const plainTextOf = (slice) => toText(slice.blocks);
154
+ /**
155
+ * A slice from pasted plain text: one paragraph per line. Identities come from
156
+ * the caller, like every other identity a live edit mints.
157
+ */
158
+ const sliceFromText = (text, mint) => ({
159
+ version: 1,
160
+ blocks: text.split(/\r\n|\r|\n/).map((line) => ({
161
+ type: "Paragraph",
162
+ id: NodeId.make(mint()),
163
+ children: [{
164
+ type: "Text",
165
+ id: NodeId.make(mint()),
166
+ text: line,
167
+ marks: []
168
+ }]
169
+ }))
170
+ });
171
+ /** The same content under fresh identities, so a paste can never collide. */
172
+ const withFreshIds = (slice, mint) => {
173
+ const freshBlock = (block) => {
174
+ const id = NodeId.make(mint());
175
+ const children = block.children.map((run) => ({
176
+ ...run,
177
+ id: NodeId.make(mint())
178
+ }));
179
+ if (block.type === "Node" && block.blocks !== void 0) return {
180
+ ...block,
181
+ id,
182
+ children,
183
+ blocks: block.blocks.map(freshBlock)
184
+ };
185
+ return {
186
+ ...block,
187
+ id,
188
+ children
189
+ };
190
+ };
191
+ return {
192
+ version: 1,
193
+ blocks: slice.blocks.map(freshBlock)
194
+ };
195
+ };
196
+ /** Encodes a slice for the clipboard. */
197
+ const serializeSlice = (slice) => JSON.stringify(encodeSlice(slice));
198
+ /** Decodes a slice, or undefined when the payload is not one this version wrote. */
199
+ const deserializeSlice = (input) => {
200
+ try {
201
+ return decodeSlice(typeof input === "string" ? JSON.parse(input) : input);
202
+ } catch {
203
+ return;
204
+ }
205
+ };
206
+ export { Slice, deserializeSlice, emptySlice, plainTextOf, serializeSlice, sliceFromText, sliceOf, withFreshIds };
207
+
208
+ //# sourceMappingURL=clipboard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clipboard.js","names":[],"sources":["../src/clipboard.ts"],"sourcesContent":["import { Schema } from 'effect'\nimport { toText } from './html.js'\nimport {\n Block,\n NodeId,\n blockAtPath,\n compareRunPlaces,\n comparePositionPlaces,\n eachBlock,\n locateBlock,\n locateRun,\n pathKey,\n type BlockPath,\n type Document,\n type RunPlace,\n type Selection,\n type Text,\n} from './document.js'\n\n/**\n * Clipboard content is semantic, not HTML (§69). A slice is a versioned,\n * identity-carrying fragment of a document: it round-trips through an\n * application's own format, and a paste mints fresh identities so nothing\n * collides with the document it lands in. HTML and plain text are interchange\n * fallbacks the adapter adds; they are not the slice.\n */\nexport const Slice = Schema.Struct({\n version: Schema.Literal(1),\n blocks: Schema.Array(Block),\n}).check(\n Schema.makeFilter(slice => {\n const ids = new Set<NodeId>()\n let duplicate = false\n eachBlock(slice.blocks, block => {\n for (const id of [block.id, ...block.children.map(run => run.id)]) {\n if (ids.has(id)) duplicate = true\n ids.add(id)\n }\n })\n return !duplicate || 'Duplicate slice identity'\n }),\n)\nexport type Slice = typeof Slice.Type\n\nexport const emptySlice: Slice = Object.freeze({ version: 1, blocks: [] })\n\nconst decodeSlice = Schema.decodeUnknownSync(Slice, { onExcessProperty: 'error' })\nconst encodeSlice = Schema.encodeSync(Slice)\n\nconst trim = (run: Text, from: number, to: number): Text => ({\n ...run,\n text: run.text.slice(from, to),\n})\n\n/** A covered span: the two endpoints, ordered, each with its offset in its run. */\ninterface CoveredSpan {\n readonly start: RunPlace & { readonly offset: number }\n readonly end: RunPlace & { readonly offset: number }\n}\n\n/** Whether a block's subtree contains an endpoint, or sits between the two. */\nconst inRange = (blockPath: BlockPath, span: CoveredSpan): boolean => {\n const contains = (place: RunPlace): boolean =>\n blockPath.length <= place.path.length &&\n blockPath.every((index, depth) => place.path[depth] === index)\n if (contains(span.start) || contains(span.end)) return true\n const first = { path: blockPath, index: 0 }\n return compareRunPlaces(first, span.start) > 0 && compareRunPlaces(first, span.end) < 0\n}\n\n/** The runs of one block that the span covers, trimmed to it. */\nconst coveredRuns = (\n block: Block,\n blockPath: BlockPath,\n span: CoveredSpan,\n): ReadonlyArray<Text> => {\n const atStart = pathKey(blockPath) === pathKey(span.start.path)\n const atEnd = pathKey(blockPath) === pathKey(span.end.path)\n if (!atStart && !atEnd) return block.children\n const from = atStart ? span.start.index : 0\n const to = atEnd ? span.end.index : block.children.length - 1\n const children: Array<Text> = []\n for (const [index, run] of block.children.entries()) {\n if (index < from || index > to) continue\n const trimFrom = index === from && atStart ? span.start.offset : 0\n const trimTo = index === to && atEnd ? span.end.offset : run.text.length\n if (trimFrom >= trimTo) continue\n children.push(trim(run, trimFrom, trimTo))\n }\n return children\n}\n\n/**\n * The covered part of a block list: the blocks the span reaches, each trimmed to\n * it. A container is kept with the children the span reaches, so copying across\n * two list items carries the list rather than two loose paragraphs.\n */\nconst coveredBlocks = (\n blocks: ReadonlyArray<Block>,\n prefix: BlockPath,\n span: CoveredSpan,\n): ReadonlyArray<Block> => {\n const covered: Array<Block> = []\n for (const [index, block] of blocks.entries()) {\n const path = [...prefix, index]\n if (!inRange(path, span)) continue\n if (block.type === 'Unknown') {\n covered.push(block)\n continue\n }\n const children = coveredRuns(block, path, span)\n if (block.type === 'Node' && block.blocks !== undefined) {\n const nested = coveredBlocks(block.blocks, path, span)\n if (children.length === 0 && nested.length === 0) continue\n covered.push({ ...block, children, blocks: nested })\n continue\n }\n if (children.length === 0) continue\n covered.push({ ...block, children })\n }\n return covered\n}\n\n/**\n * The semantic content a copy would take: a whole block for a node selection,\n * and for a range only the covered part of what it reaches, with runs trimmed to\n * the selection. Blocks the range never enters are left out, so a partial copy\n * never drags a paragraph along.\n */\nexport const sliceOf = (document: Document, selection: Selection | null): Slice | undefined => {\n if (selection === null) return undefined\n if (selection.type === 'Node') {\n const located = locateBlock(document, selection.node)\n return located === undefined ? undefined : { version: 1, blocks: [located.block] }\n }\n const anchor = locateRun(document, selection.anchor.node)\n const focus = locateRun(document, selection.focus.node)\n if (anchor === undefined || focus === undefined) return undefined\n const anchorPlace = { ...anchor, offset: selection.anchor.offset }\n const focusPlace = { ...focus, offset: selection.focus.offset }\n const span: CoveredSpan =\n comparePositionPlaces(anchorPlace, focusPlace) <= 0\n ? { start: anchorPlace, end: focusPlace }\n : { start: focusPlace, end: anchorPlace }\n // A span inside one block copies that block, as it does at the top level; a\n // span across blocks keeps the containers that hold them, so copying across\n // two list items carries the list.\n if (pathKey(span.start.path) === pathKey(span.end.path)) {\n const block = blockAtPath(document, span.start.path)\n if (block === undefined || block.type === 'Unknown') return { version: 1, blocks: [] }\n const children = coveredRuns(block, span.start.path, span)\n return { version: 1, blocks: children.length === 0 ? [] : [{ ...block, children }] }\n }\n return { version: 1, blocks: coveredBlocks(document.children, [], span) }\n}\n\n/**\n * Plain text of a slice, one line per text block. A slice and a block list have\n * the same shape, so the serializer's walk serves both.\n */\nexport const plainTextOf = (slice: Slice): string => toText(slice.blocks)\n\n/**\n * A slice from pasted plain text: one paragraph per line. Identities come from\n * the caller, like every other identity a live edit mints.\n */\nexport const sliceFromText = (text: string, mint: () => string): Slice => ({\n version: 1,\n blocks: text.split(/\\r\\n|\\r|\\n/).map(line => ({\n type: 'Paragraph' as const,\n id: NodeId.make(mint()),\n children: [\n {\n type: 'Text' as const,\n id: NodeId.make(mint()),\n text: line,\n marks: [],\n },\n ],\n })),\n})\n\n/** The same content under fresh identities, so a paste can never collide. */\nexport const withFreshIds = (slice: Slice, mint: () => string): Slice => {\n // Pre-order: a block's identity, then its runs, then its nested blocks.\n const freshBlock = (block: Block): Block => {\n const id = NodeId.make(mint())\n const children = block.children.map(run => ({ ...run, id: NodeId.make(mint()) }))\n if (block.type === 'Node' && block.blocks !== undefined) {\n return { ...block, id, children, blocks: block.blocks.map(freshBlock) }\n }\n return { ...block, id, children }\n }\n return { version: 1, blocks: slice.blocks.map(freshBlock) }\n}\n\n/** Encodes a slice for the clipboard. */\nexport const serializeSlice = (slice: Slice): string => JSON.stringify(encodeSlice(slice))\n\n/** Decodes a slice, or undefined when the payload is not one this version wrote. */\nexport const deserializeSlice = (input: unknown): Slice | undefined => {\n try {\n return decodeSlice(typeof input === 'string' ? JSON.parse(input) : input)\n } catch {\n return undefined\n }\n}\n"],"mappings":";;;;;;;;;;;AA0BA,MAAa,QAAQ,OAAO,OAAO;CACjC,SAAS,OAAO,QAAQ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O,KAAK,KAAK,CAAC;GACtB,MAAM;GACN,OAAO,CAAC;EACV,CACF;CACF,EAAE;AACJ;;AAGA,MAAa,gBAAgB,OAAc,SAA8B;CAEvE,MAAM,cAAc,UAAwB;EAC1C,MAAM,KAAK,OAAO,KAAK,KAAK,CAAC;EAC7B,MAAM,WAAW,MAAM,SAAS,KAAI,SAAQ;GAAE,GAAG;GAAK,IAAI,OAAO,KAAK,KAAK,CAAC;EAAE,EAAE;EAChF,IAAI,MAAM,SAAS,UAAU,MAAM,WAAW,KAAA,GAC5C,OAAO;GAAE,GAAG;GAAO;GAAI;GAAU,QAAQ,MAAM,OAAO,IAAI,UAAU;EAAE;EAExE,OAAO;GAAE,GAAG;GAAO;GAAI;EAAS;CAClC;CACA,OAAO;EAAE,SAAS;EAAG,QAAQ,MAAM,OAAO,IAAI,UAAU;CAAE;AAC5D;;AAGA,MAAa,kBAAkB,UAAyB,KAAK,UAAU,YAAY,KAAK,CAAC;;AAGzF,MAAa,oBAAoB,UAAsC;CACrE,IAAI;EACF,OAAO,YAAY,OAAO,UAAU,WAAW,KAAK,MAAM,KAAK,IAAI,KAAK;CAC1E,QAAQ;EACN;CACF;AACF"}
package/dist/code.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ import { Document } from "./document.js";
2
+ import { DecorationSet } from "./decoration.js";
3
+ /**
4
+ * One token in a code block's text, as offsets into that text. `kind` becomes the
5
+ * decoration's kind, so it is what a stylesheet reaches the token by — a grammar names its
6
+ * kinds `syntax-string`, `syntax-number`, and so on (§129).
7
+ */
8
+ export interface CodeToken {
9
+ readonly from: number;
10
+ readonly to: number;
11
+ readonly kind: string;
12
+ }
13
+ /** A pure read of one code block's text. A tokenizer that needs to load first is Shiki's case. */
14
+ export type CodeTokenizer = (text: string) => ReadonlyArray<CodeToken>;
15
+ /**
16
+ * Every token the given tokenizers find in the document's `CodeBlock`s, as decorations. A
17
+ * block is tokenized by the tokenizer registered for its `language` prop; one with no
18
+ * language, or one nothing is registered for, yields nothing. The registry is a `Map`
19
+ * because the language is read from the document, and a plain object would answer a
20
+ * language named `constructor` from its prototype.
21
+ *
22
+ * A token outside its block's text, one that covers nothing, or one at a fractional offset
23
+ * throws with the language that produced it: it is a tokenizer's bug, and a decoration guessed from it would
24
+ * highlight the wrong text.
25
+ */
26
+ export declare const codeDecorations: (document: Document, tokenizers: ReadonlyMap<string, CodeTokenizer>) => DecorationSet;
27
+ //# sourceMappingURL=code.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"code.d.ts","names":[],"sources":["../src/code.ts"],"mappings":";;;;;;;iBAeiB;WACN;WACA;WACA;;;YAIC,iBAAiB,iBAAiB,cAAc;;;;;;;;;;;;qBAa/C,kBAAe,UAChB,UAAQ,YACN,oBAAoB,mBAC/B"}
package/dist/code.js ADDED
@@ -0,0 +1,45 @@
1
+ import { eachBlock, positionInBlock } from "./document.js";
2
+ // src/code.ts
3
+ /**
4
+ * Code highlighting, as decorations (§124 §7, §130): the document holds a `CodeBlock`'s
5
+ * language and its plain text, and what a highlighter finds in that text is presentation,
6
+ * derived and discarded like a search match. The contract lives here because it is
7
+ * format-agnostic — a tokenizer is a function from text to ranges — while grammars, which
8
+ * are not, live in `foldkit-richtext-code` and its Shiki adapter.
9
+ */
10
+ /**
11
+ * Every token the given tokenizers find in the document's `CodeBlock`s, as decorations. A
12
+ * block is tokenized by the tokenizer registered for its `language` prop; one with no
13
+ * language, or one nothing is registered for, yields nothing. The registry is a `Map`
14
+ * because the language is read from the document, and a plain object would answer a
15
+ * language named `constructor` from its prototype.
16
+ *
17
+ * A token outside its block's text, one that covers nothing, or one at a fractional offset
18
+ * throws with the language that produced it: it is a tokenizer's bug, and a decoration guessed from it would
19
+ * highlight the wrong text.
20
+ */
21
+ const codeDecorations = (document, tokenizers) => {
22
+ const found = [];
23
+ eachBlock(document.children, (block) => {
24
+ if (block.type !== "Node" || block.kind !== "CodeBlock") return;
25
+ const language = block.props.language;
26
+ if (typeof language !== "string") return;
27
+ const tokenizer = tokenizers.get(language);
28
+ if (tokenizer === void 0) return;
29
+ const text = block.children.map((run) => run.text).join("");
30
+ for (const token of tokenizer(text)) {
31
+ const from = Number.isInteger(token.from) && Number.isInteger(token.to) && token.from < token.to ? positionInBlock(block, token.from) : void 0;
32
+ const to = from === void 0 ? void 0 : positionInBlock(block, token.to);
33
+ if (from === void 0 || to === void 0) throw new RangeError(`The ${language} tokenizer produced ${token.kind} at [${token.from}, ${token.to}) in a block of ${text.length} characters`);
34
+ found.push({
35
+ from,
36
+ to,
37
+ kind: token.kind
38
+ });
39
+ }
40
+ });
41
+ return found;
42
+ };
43
+ export { codeDecorations };
44
+
45
+ //# sourceMappingURL=code.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"code.js","names":[],"sources":["../src/code.ts"],"sourcesContent":["/**\n * Code highlighting, as decorations (§124 §7, §130): the document holds a `CodeBlock`'s\n * language and its plain text, and what a highlighter finds in that text is presentation,\n * derived and discarded like a search match. The contract lives here because it is\n * format-agnostic — a tokenizer is a function from text to ranges — while grammars, which\n * are not, live in `foldkit-richtext-code` and its Shiki adapter.\n */\nimport { eachBlock, positionInBlock, type Document } from './document.js'\nimport type { Decoration, DecorationSet } from './decoration.js'\n\n/**\n * One token in a code block's text, as offsets into that text. `kind` becomes the\n * decoration's kind, so it is what a stylesheet reaches the token by — a grammar names its\n * kinds `syntax-string`, `syntax-number`, and so on (§129).\n */\nexport interface CodeToken {\n readonly from: number\n readonly to: number\n readonly kind: string\n}\n\n/** A pure read of one code block's text. A tokenizer that needs to load first is Shiki's case. */\nexport type CodeTokenizer = (text: string) => ReadonlyArray<CodeToken>\n\n/**\n * Every token the given tokenizers find in the document's `CodeBlock`s, as decorations. A\n * block is tokenized by the tokenizer registered for its `language` prop; one with no\n * language, or one nothing is registered for, yields nothing. The registry is a `Map`\n * because the language is read from the document, and a plain object would answer a\n * language named `constructor` from its prototype.\n *\n * A token outside its block's text, one that covers nothing, or one at a fractional offset\n * throws with the language that produced it: it is a tokenizer's bug, and a decoration guessed from it would\n * highlight the wrong text.\n */\nexport const codeDecorations = (\n document: Document,\n tokenizers: ReadonlyMap<string, CodeTokenizer>,\n): DecorationSet => {\n const found: Array<Decoration> = []\n eachBlock(document.children, block => {\n if (block.type !== 'Node' || block.kind !== 'CodeBlock') return\n const language = block.props.language\n if (typeof language !== 'string') return\n const tokenizer = tokenizers.get(language)\n if (tokenizer === undefined) return\n const text = block.children.map(run => run.text).join('')\n for (const token of tokenizer(text)) {\n const covers =\n Number.isInteger(token.from) && Number.isInteger(token.to) && token.from < token.to\n const from = covers ? positionInBlock(block, token.from) : undefined\n const to = from === undefined ? undefined : positionInBlock(block, token.to)\n if (from === undefined || to === undefined) {\n throw new RangeError(\n `The ${language} tokenizer produced ${token.kind} at [${token.from}, ${token.to}) in a block of ${text.length} characters`,\n )\n }\n found.push({ from, to, kind: token.kind })\n }\n })\n return found\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAmCA,MAAa,mBACX,UACA,eACkB;CAClB,MAAM,QAA2B,CAAC;CAClC,UAAU,SAAS,WAAU,UAAS;EACpC,IAAI,MAAM,SAAS,UAAU,MAAM,SAAS,aAAa;EACzD,MAAM,WAAW,MAAM,MAAM;EAC7B,IAAI,OAAO,aAAa,UAAU;EAClC,MAAM,YAAY,WAAW,IAAI,QAAQ;EACzC,IAAI,cAAc,KAAA,GAAW;EAC7B,MAAM,OAAO,MAAM,SAAS,KAAI,QAAO,IAAI,IAAI,CAAC,CAAC,KAAK,EAAE;EACxD,KAAK,MAAM,SAAS,UAAU,IAAI,GAAG;GAGnC,MAAM,OADJ,OAAO,UAAU,MAAM,IAAI,KAAK,OAAO,UAAU,MAAM,EAAE,KAAK,MAAM,OAAO,MAAM,KAC7D,gBAAgB,OAAO,MAAM,IAAI,IAAI,KAAA;GAC3D,MAAM,KAAK,SAAS,KAAA,IAAY,KAAA,IAAY,gBAAgB,OAAO,MAAM,EAAE;GAC3E,IAAI,SAAS,KAAA,KAAa,OAAO,KAAA,GAC/B,MAAM,IAAI,WACR,OAAO,SAAS,sBAAsB,MAAM,KAAK,OAAO,MAAM,KAAK,IAAI,MAAM,GAAG,kBAAkB,KAAK,OAAO,YAChH;GAEF,MAAM,KAAK;IAAE;IAAM;IAAI,MAAM,MAAM;GAAK,CAAC;EAC3C;CACF,CAAC;CACD,OAAO;AACT"}