Skip to content

Commit

Permalink
Handle nested plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
MaPoKen committed Feb 20, 2024
1 parent d8f56ba commit f17d94b
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 197 deletions.
2 changes: 2 additions & 0 deletions src/components/SlateEditor/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
TableCellElement,
TableElement,
TableHeadElement,
TableHeaderCellElement,
TableRowElement,
} from "./plugins/table/interfaces";
import { DisclaimerElement } from "./plugins/uuDisclaimer/types";
Expand Down Expand Up @@ -96,6 +97,7 @@ declare module "slate" {
| TableCaptionElement
| TableRowElement
| TableCellElement
| TableHeaderCellElement
| TableHeadElement
| TableBodyElement
| RelatedElement
Expand Down
12 changes: 12 additions & 0 deletions src/components/SlateEditor/plugins/PluginFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export const createPluginFactory =
const [node] = entry;
if (Element.isElement(node) && node.type === type) {
const defaultNormalized = normalizerConfig ? defaultBlockNormalizer(editor, entry, normalizerConfig) : false;
if (config.debugSlate && defaultNormalized) {
/* eslint-disable-next-line */
console.debug(`[NORMALIZING] ${type} with method: DEFAULT BLOCK NORMALIZER `);
}
const normalized = normalize?.reduceRight((acc, { description, normalize }) => {
const isNormalized = normalize(entry as NodeEntry<Extract<Element, { type: T }>>, editor);
if (config.debugSlate && isNormalized) {
Expand Down Expand Up @@ -78,3 +82,11 @@ export const createPluginFactory =

return editor;
};

export const createPlugin = <T extends ElementType>(data: Plugin<T>) => {
const plugins: any[] = [data];
if (data.childPlugins) {
data.childPlugins.forEach((plg) => plugins.push(plg));
}
return plugins.map(createPluginFactory);
};
4 changes: 2 additions & 2 deletions src/components/SlateEditor/plugins/keyFigure/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { NormalizerConfig } from "../../utils/defaultNormalizer";
import { afterOrBeforeTextBlockElement } from "../../utils/normalizationHelpers";
import { TYPE_NDLA_EMBED } from "../embed/types";
import { TYPE_PARAGRAPH } from "../paragraph/types";
import { createPluginFactory } from "../PluginFactory";
import { createPlugin, createPluginFactory } from "../PluginFactory";

export interface KeyFigureElement {
type: "key-figure";
Expand Down Expand Up @@ -53,7 +53,7 @@ export const keyFigureSerializer: SlateSerializer = {
},
};

export const keyFigurePlugin = createPluginFactory<KeyFigureElement>({
export const keyFigurePlugin = createPlugin<KeyFigureElement["type"]>({
normalizerConfig: normalizerConfig,
type: TYPE_KEY_FIGURE,
isVoid: true,
Expand Down
4 changes: 2 additions & 2 deletions src/components/SlateEditor/plugins/paragraph/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { KEY_ENTER } from "../../utils/keys";
import { TYPE_BREAK } from "../break/types";
import { TYPE_LIST_ITEM } from "../list/types";
import { TYPE_NOOP } from "../noop/types";
import { createPluginFactory } from "../PluginFactory";
import { createPlugin, createPluginFactory } from "../PluginFactory";
import { TYPE_TABLE_CELL } from "../table/types";

export interface ParagraphElement {
Expand Down Expand Up @@ -101,7 +101,7 @@ export const paragraphSerializer: SlateSerializer = {
},
};

export const paragraphPlugin = createPluginFactory<ParagraphElement>({
export const paragraphPlugin = createPlugin<ParagraphElement["type"]>({
type: TYPE_PARAGRAPH,
onKeyDown: {
[KEY_ENTER]: onEnter,
Expand Down
4 changes: 2 additions & 2 deletions src/components/SlateEditor/plugins/table/handleKeyDown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import { TableBodyElement, TableCellElement, TableElement, TableHeadElement, Tab
import { getTableAsMatrix } from "./matrix";
import { findCellCoordinate } from "./matrixHelpers";
import { createIdenticalRow, isTable, isTableBody, isTableCell, isTableHead } from "./slateHelpers";
import getCurrentBlock from "../../utils/getCurrentBlock";
import { TYPE_TABLE_CAPTION } from "./types";
import getCurrentBlock from "../../utils/getCurrentBlock";

const getTableCell = (editor: Editor) => {
const [cellEntry] = Editor.nodes<TableCellElement>(editor, {
at: editor?.selection?.anchor.path,
match: (node) => isTableCell(node),
});

return !!cellEntry ? cellEntry : undefined;
return cellEntry ? cellEntry : undefined;
};

const getTableBody = (editor: Editor) => {
Expand Down
Loading

0 comments on commit f17d94b

Please sign in to comment.