File size: 7,079 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import type { CellPlugin } from '@react-page/editor';
import { lazyLoad } from '@react-page/editor';
import React from 'react';
import ReadOnlySlate from './components/ReadOnlySlate';
import { defaultTranslations } from './default/settings';
import { HtmlToSlate } from './htmlToSlate';
import v002 from './migrations/v002';
import v003 from './migrations/v003';
import v004 from './migrations/v004';
import * as pluginFactories from './pluginFactories/index';
import defaultPlugins from './plugins/index';
import type { InitialSlateStateDef } from './types/initialSlateState';
import type { SlatePluginCollection } from './types/SlatePlugin';
import type { SlateState } from './types/state';
import { getTextContents } from './utils/getTextContent';
import makeSlatePluginsFromDef from './utils/makeSlatePluginsFromDef';
import transformInitialSlateState from './utils/transformInitialSlateState';
import { useSafeSetState } from './utils/useSafeSetState';

const slatePlugins = defaultPlugins;

export {
  defaultPlugins,
  slatePlugins,
  pluginFactories,
  HtmlToSlate,
  SlateState,
};

const SlateEditor = lazyLoad(() => import('./components/SlateEditor'));
const Subject = lazyLoad(() => import('@mui/icons-material/Subject'));
const Controls = lazyLoad(() => import('./components/Controls'));
const SlateProvider = lazyLoad(() => import('./components/SlateProvider'));

const migrations = [v002, v003, v004];
type SlateDefinition<TPlugins extends SlatePluginCollection> = {
  icon: JSX.Element;
  plugins: TPlugins;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  defaultPluginType: string;
  title?: string;
  description?: string;
  id: string;
  version: number;
  translations: typeof defaultTranslations;
  migrations: typeof migrations;
  allowInlineNeighbours: boolean;
  hideInMenu?: boolean;
};
type DefaultPlugins = typeof defaultPlugins;
type DefaultSlateDefinition = SlateDefinition<DefaultPlugins>;
export type CreateDataCustomizer<TPlugins> = ({
  plugins,
}: {
  plugins: TPlugins;
}) => InitialSlateStateDef;

export const DEFAULT_SLATE_PLUGIN_ID = 'ory/editor/core/content/slate';
const defaultConfig: DefaultSlateDefinition = {
  icon: <Subject />,
  plugins: defaultPlugins,
  defaultPluginType: 'PARAGRAPH/PARAGRAPH',

  id: DEFAULT_SLATE_PLUGIN_ID,
  version: 1,
  translations: defaultTranslations,
  migrations,

  allowInlineNeighbours: true,
};

type CreateSlateData<TPlugins> = (
  custom?: CreateDataCustomizer<TPlugins>
) => SlateState;
export type SlateCellPlugin<
  TPlugins extends SlatePluginCollection = DefaultPlugins
> = CellPlugin<SlateState, Omit<SlateState, 'selection'>> & {
  createData: CreateSlateData<TPlugins>;
  createDataFromHtml: (html: string) => Promise<SlateState>;
  /**
   * @deprecated, use createData
   */
  createInitialSlateState: CreateSlateData<TPlugins>;
};

export type SlateCustomizeFunction<TPlugins extends SlatePluginCollection> = (
  def: DefaultSlateDefinition
) => SlateDefinition<TPlugins>;

function plugin<TPlugins extends SlatePluginCollection = DefaultPlugins>(
  customize?: SlateCustomizeFunction<TPlugins>
): SlateCellPlugin<TPlugins> {
  const settings = (
    customize ? customize(defaultConfig) : defaultConfig
  ) as SlateDefinition<TPlugins>;

  const createData = (customizer?: CreateDataCustomizer<TPlugins>) => {
    if (!customizer) {
      return { slate: [] };
    }
    return transformInitialSlateState(
      customizer({ plugins: settings.plugins })
    );
  };

  const createInitialData = () =>
    createData(({ plugins }) => ({
      children: [
        {
          plugin: plugins.paragraphs.paragraph,
          children: [''],
        },
      ],
    }));

  // plugins should be flatten
  // NEW: to make it easier to manage and group plugins,
  // they now need to be an object of object with group and keys, see type SlatePluginCollection
  const plugins = makeSlatePluginsFromDef(settings.plugins);
  const htmlToSlate = HtmlToSlate({ plugins });

  return {
    Renderer: (props) => {
      const allProps = {
        ...props,
        plugins,
        translations: settings.translations,
        defaultPluginType: settings.defaultPluginType,
      };

      /* we need a small fix to avoid flashing when SSR in edit mode:
      we code split the Provider AND the editor version, but we have to make sure to not render the editor without the provider:
      */
      const [providerLoaded, setProviderLoaded] = useSafeSetState(false);
      if (!props.readOnly && !providerLoaded) {
        SlateProvider.load().then(() => setProviderLoaded(true));
      }

      if (props.readOnly || !providerLoaded) {
        return <ReadOnlySlate {...allProps} />;
      }
      return (
        <SlateEditor {...allProps} fallback={<ReadOnlySlate {...allProps} />} />
      );
    },

    Provider: (props) => (
      <SlateProvider
        {...props}
        plugins={plugins}
        translations={settings.translations}
        defaultPluginType={settings.defaultPluginType}
        fallback={<>{props.children}</>}
      />
    ),
    // we no longer require the provider in read only mode thanks to the static renderer:
    disableProviderInReadOnly: true,

    controls: {
      type: 'custom',
      Component: (props) => (
        <Controls
          {...props}
          plugins={plugins}
          translations={settings.translations}
        />
      ),
    },

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    id: settings.id || (settings as any).name,
    version: settings.version,
    icon: settings.icon,
    title: settings.title || settings.translations.pluginName,
    description:
      settings.description || settings.translations.pluginDescription,
    hideInMenu: settings.hideInMenu,
    allowInlineNeighbours: settings.allowInlineNeighbours,
    allowClickInside: true,

    // disable default hotkeys
    handleRemoveHotKey: () => Promise.reject(),
    handleFocusPreviousHotKey: () => Promise.reject(),
    handleFocusNextHotKey: (e, node) => Promise.reject(),

    createInitialData,
    createInitialState: createInitialData,
    createInitialSlateState: createData,
    createData: createData,
    createDataFromHtml: htmlToSlate,
    getTextContents: (data) =>
      getTextContents(data.slate, { slatePlugins: plugins }),
    // remove selection
    serialize: ({ slate, selection, ...rest } = { slate: [] }) => ({
      slate,
      ...rest,
    }),
    cellClassName: 'slate',
    unserialize: (s) => {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      if ((s as any)?.importFromHtml) {
        // this is no longer supported, but we do not delete it
        return {
          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          importFromHtml: (s as any).importFromHtml,
          ...s,
          ...createInitialData(),
        };
      }
      if (s?.slate) {
        return {
          ...s,
          selection: null,
        };
      }

      return createInitialData();
    },

    migrations: settings.migrations,
  };
}

export default plugin;