document.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { Editor } from 'slate';
  2. export enum BlockType {
  3. PageBlock = 'page',
  4. HeadingBlock = 'heading',
  5. TextBlock = 'text',
  6. TodoListBlock = 'todo_list',
  7. BulletedListBlock = 'bulleted_list',
  8. NumberedListBlock = 'numbered_list',
  9. CodeBlock = 'code',
  10. EmbedBlock = 'embed',
  11. QuoteBlock = 'quote',
  12. CalloutBlock = 'callout',
  13. DividerBlock = 'divider',
  14. MediaBlock = 'media',
  15. TableBlock = 'table',
  16. ColumnBlock = 'column',
  17. }
  18. export interface HeadingBlockData extends TextBlockData {
  19. level: number;
  20. }
  21. export interface TodoListBlockData extends TextBlockData {
  22. checked: boolean;
  23. }
  24. export interface QuoteBlockData extends TextBlockData {
  25. size: 'default' | 'large';
  26. }
  27. export interface TextBlockData {
  28. delta: TextDelta[];
  29. }
  30. export type PageBlockData = TextBlockData;
  31. export type BlockData<Type> = Type extends BlockType.HeadingBlock
  32. ? HeadingBlockData
  33. : Type extends BlockType.PageBlock
  34. ? PageBlockData
  35. : Type extends BlockType.TodoListBlock
  36. ? TodoListBlockData
  37. : Type extends BlockType.QuoteBlock
  38. ? QuoteBlockData
  39. : TextBlockData;
  40. export interface NestedBlock<Type = any> {
  41. id: string;
  42. type: BlockType;
  43. data: BlockData<Type>;
  44. parent: string | null;
  45. children: string;
  46. }
  47. export interface TextDelta {
  48. insert: string;
  49. attributes?: Record<string, string | boolean>;
  50. }
  51. export enum BlockActionType {
  52. Insert = 0,
  53. Update = 1,
  54. Delete = 2,
  55. Move = 3,
  56. }
  57. export interface DeltaItem {
  58. action: 'inserted' | 'removed' | 'updated';
  59. payload: {
  60. id: string;
  61. value?: NestedBlock | string[];
  62. };
  63. }
  64. export type Node = NestedBlock;
  65. export interface SelectionPoint {
  66. path: [number, number];
  67. offset: number;
  68. }
  69. export interface TextSelection {
  70. anchor: SelectionPoint;
  71. focus: SelectionPoint;
  72. }
  73. export interface DocumentData {
  74. rootId: string;
  75. // map of block id to block
  76. nodes: Record<string, Node>;
  77. // map of block id to children block ids
  78. children: Record<string, string[]>;
  79. }
  80. export interface DocumentState {
  81. // map of block id to block
  82. nodes: Record<string, Node>;
  83. // map of block id to children block ids
  84. children: Record<string, string[]>;
  85. // selected block ids
  86. selections: string[];
  87. // map of block id to text selection
  88. textSelections: Record<string, TextSelection>;
  89. }
  90. export enum ChangeType {
  91. BlockInsert,
  92. BlockUpdate,
  93. BlockDelete,
  94. ChildrenMapInsert,
  95. ChildrenMapUpdate,
  96. ChildrenMapDelete,
  97. }
  98. export interface BlockPBValue {
  99. id: string;
  100. ty: string;
  101. parent: string;
  102. children: string;
  103. data: string;
  104. }
  105. export type TextBlockKeyEventHandlerParams = [React.KeyboardEvent<HTMLDivElement>, Editor];