File size: 2,188 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { Node } from 'slate';
import type {
  InitialSlateStateDef,
  SlateDefNode,
  SlatePluginNode,
} from '../types/initialSlateState';
import type { SlatePlugin } from '../types/SlatePlugin';
import flattenDeep from './flattenDeep';

/**
 * FIXME: transformInitialSlateState does some polymorphic type magic, so that it is directly
 * compatible with the shipped default plugins or plugins created with pluginFactories
 *
 * This is a bit ugly and might be hard to understand,
 * but on the other hand its easy to use for developers that use this library
 *
 * Basicaly what we do is to unpack the factory or its result until we find
 * - object: "block" | "inline" | "mark"
 * - type: "SOMESTRING"
 *
 * We might revisit this in the future.
 *
 */

const transformChildren = (defNodes: SlateDefNode[]): Node[] =>
  defNodes.map((defNode) => {
    if ((defNode as SlatePluginNode).plugin) {
      const defPluginNode: SlatePluginNode = defNode as SlatePluginNode;
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      const slatePluginOrList = (defPluginNode.plugin as any).toPlugin
        ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
          (defPluginNode.plugin as any).toPlugin()
        : defPluginNode.plugin;

      // the result of plugin.toPlugin might be an array, e.g. the list plugin is an array, because it defines ul, li AND indention-options on the same plugin
      const firstComponentPlugin = flattenDeep<SlatePlugin>(
        slatePluginOrList
      ).find((plugin) => plugin.pluginType === 'component' || plugin);
      if (
        firstComponentPlugin &&
        firstComponentPlugin.pluginType === 'component'
      ) {
        return {
          type: firstComponentPlugin.type,
          ...(defPluginNode.data ?? {}),
          children: defPluginNode.children
            ? transformChildren(defPluginNode.children)
            : [],
        } as any;
      } else {
        return null;
      }
    } else if (typeof defNode === 'string') {
      return {
        text: defNode as string,
      };
    }
  });

export default (def: InitialSlateStateDef) => ({
  slate: transformChildren(def.children),
});