All files / json-crdt-extensions/peritext/registry registry.ts

75.86% Statements 22/29
0% Branches 0/6
28.57% Functions 2/7
75.86% Lines 22/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 742x     2x 2x 2x   2x         2x   2x   1x 2x       2x             2x             2x 2x 2x 2x 2x 2x 2x 2x 2x   2x       2x                                                
import {s} from '../../../json-crdt-patch';
import type {JsonNodeView} from '../../../json-crdt/nodes';
import type {SchemaToJsonNode} from '../../../json-crdt/schema/types';
import {CommonSliceType} from '../slice';
import {SliceBehavior} from '../slice/constants';
import {SliceRegistry} from './SliceRegistry';
 
const undefSchema = s.con(undefined);
 
/**
 * Default annotation type registry.
 */
export const registry = new SliceRegistry();
 
registry.def(CommonSliceType.i, undefSchema, SliceBehavior.One, true, {
  fromHtml: {
    i: () => [CommonSliceType.i, null],
    em: () => [CommonSliceType.i, null],
  },
});
 
registry.def(CommonSliceType.b, undefSchema, SliceBehavior.One, true, {
  fromHtml: {
    b: () => [CommonSliceType.b, null],
    strong: () => [CommonSliceType.b, null],
  },
});
 
registry.def(CommonSliceType.s, undefSchema, SliceBehavior.One, true, {
  fromHtml: {
    s: () => [CommonSliceType.s, null],
    strike: () => [CommonSliceType.s, null],
  },
});
 
registry.def(CommonSliceType.u, undefSchema, SliceBehavior.One, true);
registry.def(CommonSliceType.code, undefSchema, SliceBehavior.One, true);
registry.def(CommonSliceType.mark, undefSchema, SliceBehavior.One, true);
registry.def(CommonSliceType.kbd, undefSchema, SliceBehavior.One, true);
registry.def(CommonSliceType.del, undefSchema, SliceBehavior.One, true);
registry.def(CommonSliceType.ins, undefSchema, SliceBehavior.One, true);
registry.def(CommonSliceType.sup, undefSchema, SliceBehavior.One, true);
registry.def(CommonSliceType.sub, undefSchema, SliceBehavior.One, true);
registry.def(CommonSliceType.math, undefSchema, SliceBehavior.One, true);
 
const aSchema = s.obj({
  href: s.str<string>(''),
  title: s.str<string>(''),
});
registry.def(CommonSliceType.a, aSchema, SliceBehavior.Many, true, {
  fromHtml: {
    a: (jsonml) => {
      const attr = jsonml[1] || {};
      const data: JsonNodeView<SchemaToJsonNode<typeof aSchema>> = {
        href: attr.href ?? '',
        title: attr.title ?? '',
      };
      return [CommonSliceType.a, {data, inline: true}];
    },
  },
});
 
// TODO: add more default annotations
// comment = SliceTypeCon.comment,
// font = SliceTypeCon.font,
// col = SliceTypeCon.col,
// bg = SliceTypeCon.bg,
// hidden = SliceTypeCon.hidden,
// footnote = SliceTypeCon.footnote,
// ref = SliceTypeCon.ref,
// iaside = SliceTypeCon.iaside,
// iembed = SliceTypeCon.iembed,
// bookmark = SliceTypeCon.bookmark,