Skip to content

Commit 8434117

Browse files
committed
feat(json-crdt-extensions): 🎸 harden registry usage, make it required
1 parent 15ee212 commit 8434117

File tree

5 files changed

+19
-28
lines changed

5 files changed

+19
-28
lines changed

src/json-crdt-extensions/peritext/registry/registry.ts renamed to src/json-crdt-extensions/peritext/registry/registerCommon.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import {s} from '../../../json-crdt-patch';
22
import {SliceBehavior, SliceTypeCon as TAG} from '../slice/constants';
3-
import {SliceRegistry, SliceRegistryEntry, type TagType} from './SliceRegistry';
3+
import {SliceRegistry, type TypeTag} from './SliceRegistry';
4+
import {SliceRegistryEntry} from './SliceRegistryEntry';
45
import type {JsonNodeView} from '../../../json-crdt/nodes';
56
import type {SchemaToJsonNode} from '../../../json-crdt/schema/types';
67
import type {PeritextMlElement} from '../block/types';
78

8-
export const createRegistry = () => {
9-
const registry = new SliceRegistry();
10-
const undefSchema = s.con(undefined);
9+
const undefSchema = s.con(undefined);
1110

11+
export const registerCommon = (registry: SliceRegistry) => {
1212
// --------------------------------------- Inline elements with "One" behavior
1313

14-
const i0 = <Tag extends TagType = TagType>(
14+
const i0 = <Tag extends TypeTag = TypeTag>(
1515
tag: Tag,
1616
fromHtml?: SliceRegistryEntry<SliceBehavior.One, Tag, typeof undefSchema>['fromHtml'],
1717
): void => {
1818
registry.add(new SliceRegistryEntry(SliceBehavior.One, tag, undefSchema, false, void 0, fromHtml));
1919
};
2020

21-
const i1 = <Tag extends TagType = TagType>(tag: Tag, htmlTags: string[]): void => {
21+
const i1 = <Tag extends TypeTag = TypeTag>(tag: Tag, htmlTags: string[]): void => {
2222
const fromHtml = {} as Record<any, any>;
2323
for (const htmlTag of htmlTags) fromHtml[htmlTag] = () => [tag, null];
2424
i0(tag, fromHtml);
@@ -78,7 +78,7 @@ export const createRegistry = () => {
7878
},
7979
);
8080

81-
const b0 = <Tag extends TagType = TagType>(tag: Tag, container: boolean) => {
81+
const b0 = <Tag extends TypeTag = TypeTag>(tag: Tag, container: boolean) => {
8282
registry.add(new SliceRegistryEntry(SliceBehavior.Marker, tag, commonBlockSchema, container));
8383
};
8484

@@ -114,11 +114,4 @@ export const createRegistry = () => {
114114
// b0(TAG.collapse, true);
115115
// b0(TAG.note, true);
116116
// b0(TAG.mathblock, false);
117-
118-
return registry;
119117
};
120-
121-
/**
122-
* Default annotation type registry.
123-
*/
124-
export const registry = createRegistry();

src/json-crdt-extensions/peritext/transfer/PeritextDataTransfer.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {registry as defaultRegistry} from '../registry/registry';
21
import type {Range} from '../rga/Range';
32
import type {ViewRange} from '../editor/types';
43
import type {SliceRegistry} from '../registry/SliceRegistry';
@@ -15,7 +14,7 @@ export type PeritextDataTransferMarkdownExportTools = typeof import('./export-ma
1514
export type PeritextDataTransferMarkdownImportTools = typeof import('./import-markdown');
1615

1716
export interface PeritextDataTransferOpts {
18-
registry?: SliceRegistry;
17+
registry: SliceRegistry;
1918
htmlExport?: PeritextDataTransferHtmlExportTools;
2019
htmlImport?: PeritextDataTransferHtmlImportTools;
2120
mdExport?: PeritextDataTransferMarkdownExportTools;
@@ -41,7 +40,7 @@ export interface ClipboardImport {
4140
export class PeritextDataTransfer<T = string> {
4241
constructor(
4342
public readonly txt: Peritext<T>,
44-
public readonly opts: PeritextDataTransferOpts = {},
43+
public readonly opts: PeritextDataTransferOpts,
4544
) {}
4645

4746
// ------------------------------------------------------------------ exports
@@ -132,7 +131,7 @@ export class PeritextDataTransfer<T = string> {
132131
}
133132

134133
private _imp<D>(pos: number, data: D, transform: (data: D, registry: SliceRegistry) => PeritextMlNode): number {
135-
const registry = this.opts.registry ?? defaultRegistry;
134+
const registry = this.opts.registry;
136135
const json = transform(data, registry);
137136
return this.fromJson(pos, json);
138137
}
@@ -191,7 +190,7 @@ export class PeritextDataTransfer<T = string> {
191190
return range.start.viewPos();
192191
};
193192
if (html) {
194-
const [view, style] = this.htmlI().importHtml(html);
193+
const [view, style] = this.htmlI().importHtml(html, txt.editor.registry);
195194
if (style) {
196195
txt.editor.importStyle(range, style);
197196
return 0;

src/json-crdt-extensions/peritext/transfer/create.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const create = (txt: Peritext) => {
1111
htmlImport,
1212
mdExport,
1313
mdImport,
14+
registry: txt.editor.registry,
1415
});
1516
return transfer;
1617
};

src/json-crdt-extensions/peritext/transfer/import-html.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {html as _html} from 'very-small-parser/lib/html';
22
import {fromHast as _fromHast} from 'very-small-parser/lib/html/json-ml/fromHast';
33
import {SliceTypeName} from '../slice';
4-
import {registry as defaultRegistry} from '../registry/registry';
54
import {SliceBehavior, SliceHeaderShift} from '../slice/constants';
65
import {Anchor} from '../rga/constants';
76
import {toPlainText} from 'very-small-parser/lib/toPlainText';
@@ -98,7 +97,7 @@ const BLOCK_TAGS_REWRITE = new Map<string, string>([
9897
// HTML elements to rewrite as different inline elements.
9998
const INLINE_TAGS_REWRITE = new Map<string, string>([['span', '']]);
10099

101-
export const fromJsonMl = (jsonml: JsonMlNode, registry: SliceRegistry = defaultRegistry): PeritextMlNode => {
100+
export const fromJsonMl = (jsonml: JsonMlNode, registry: SliceRegistry): PeritextMlNode => {
102101
if (typeof jsonml === 'string') return jsonml;
103102
let tag = jsonml[0];
104103
let inlineHtmlTag = false;
@@ -143,12 +142,12 @@ export const fromJsonMl = (jsonml: JsonMlNode, registry: SliceRegistry = default
143142
return node;
144143
};
145144

146-
export const fromHast = (hast: THtmlToken, registry?: SliceRegistry): PeritextMlNode => {
145+
export const fromHast = (hast: THtmlToken, registry: SliceRegistry): PeritextMlNode => {
147146
const jsonml = _fromHast(hast);
148147
return fromJsonMl(jsonml, registry);
149148
};
150149

151-
export const fromHtml = (html: string, registry?: SliceRegistry): PeritextMlNode => {
150+
export const fromHtml = (html: string, registry: SliceRegistry): PeritextMlNode => {
152151
const hast = _html.parsef(html);
153152
return fromHast(hast, registry);
154153
};
@@ -184,11 +183,11 @@ const getExportData = (html: string): [jsonml: undefined | JsonMlNode, exportDat
184183
return [jsonml];
185184
};
186185

187-
export const importHtml = (html: string): [view?: ViewRange, style?: ViewStyle[]] => {
186+
export const importHtml = (html: string, registry: SliceRegistry): [view?: ViewRange, style?: ViewStyle[]] => {
188187
const [jsonml, data] = getExportData(html);
189188
if (data?.style) return [void 0, data.style];
190189
if (data?.view) return [data.view];
191-
const node = fromJsonMl(jsonml!);
190+
const node = fromJsonMl(jsonml!, registry);
192191
return [toViewRange(node)];
193192
};
194193

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import {toHast} from 'very-small-parser/lib/markdown/block/toHast';
22
import {block} from 'very-small-parser/lib/markdown/block';
33
import {fromHast as _fromHast} from 'very-small-parser/lib/html/json-ml/fromHast';
4-
import {registry as defaultRegistry} from '../registry/registry';
54
import {fromHast} from './import-html';
65
import type {IRoot} from 'very-small-parser/lib/markdown/block/types';
76
import type {PeritextMlNode} from '../block/types';
87
import type {SliceRegistry} from '../registry/SliceRegistry';
98

10-
export const fromMdast = (mdast: IRoot, registry: SliceRegistry = defaultRegistry): PeritextMlNode => {
9+
export const fromMdast = (mdast: IRoot, registry: SliceRegistry): PeritextMlNode => {
1110
const hast = toHast(mdast);
1211
const node = fromHast(hast, registry);
1312
return node;
1413
};
1514

16-
export const fromMarkdown = (markdown: string, registry?: SliceRegistry): PeritextMlNode => {
15+
export const fromMarkdown = (markdown: string, registry: SliceRegistry): PeritextMlNode => {
1716
const mdast = block.parsef(markdown);
1817
return fromMdast(mdast, registry);
1918
};

0 commit comments

Comments
 (0)