@udecode/plate-indent
Version:
Indent plugin for Plate
1 lines • 7.83 kB
Source Map (JSON)
{"version":3,"sources":["../src/lib/BaseIndentPlugin.ts","../src/lib/withIndent.ts","../src/lib/BaseTextIndentPlugin.ts","../src/lib/transforms/setIndent.ts","../src/lib/transforms/indent.ts","../src/lib/transforms/outdent.ts"],"sourcesContent":["import {\n type PluginConfig,\n type TElement,\n BaseParagraphPlugin,\n createTSlatePlugin,\n} from '@udecode/plate';\n\nimport { withIndent } from './withIndent';\n\nexport type IndentConfig = PluginConfig<\n 'indent',\n {\n /** Maximum number of indentation. */\n indentMax?: number;\n /**\n * Indentation offset used in `(offset * element.indent) + unit`.\n *\n * @default 40\n */\n offset?: number;\n /**\n * Indentation unit used in `(offset * element.indent) + unit`.\n *\n * @default 'px'\n */\n unit?: string;\n }\n>;\n\nexport interface TIndentElement extends TElement {\n indent: number;\n}\n\nexport const BaseIndentPlugin = createTSlatePlugin<IndentConfig>({\n key: 'indent',\n inject: {\n isBlock: true,\n nodeProps: {\n nodeKey: 'indent',\n styleKey: 'marginLeft',\n transformNodeValue: ({ getOptions, nodeValue }) => {\n const { offset, unit } = getOptions();\n\n return nodeValue * offset! + unit!;\n },\n },\n targetPlugins: [BaseParagraphPlugin.key],\n },\n options: {\n offset: 24,\n unit: 'px',\n },\n}).overrideEditor(withIndent);\n","import type { OverrideEditor } from '@udecode/plate';\n\nimport { getInjectMatch } from '@udecode/plate';\n\nimport type { IndentConfig, TIndentElement } from './BaseIndentPlugin';\n\n/**\n * - `node.indent` can not exceed `indentMax`\n * - `node.indent` is unset if `node.type` is not in `types`\n */\nexport const withIndent: OverrideEditor<IndentConfig> = ({\n editor,\n getOptions,\n plugin,\n tf: { normalizeNode },\n}) => {\n return {\n transforms: {\n normalizeNode([node, path]) {\n const { indentMax } = getOptions();\n\n const element = node as TIndentElement;\n const { type } = element;\n\n const match = getInjectMatch(editor, plugin);\n\n if (type) {\n if (match(element, path)) {\n if (indentMax && element.indent && element.indent > indentMax) {\n editor.tf.setNodes({ indent: indentMax }, { at: path });\n\n return;\n }\n } else if (element.indent) {\n editor.tf.unsetNodes('indent', { at: path });\n\n return;\n }\n }\n\n return normalizeNode([node, path]);\n },\n },\n };\n};\n","import {\n type PluginConfig,\n BaseParagraphPlugin,\n createTSlatePlugin,\n} from '@udecode/plate';\n\nimport type { IndentConfig } from './BaseIndentPlugin';\n\nexport type TextIndentConfig = PluginConfig<\n 'textIndent',\n IndentConfig['options']\n>;\n\nexport const BaseTextIndentPlugin = createTSlatePlugin<TextIndentConfig>({\n key: 'textIndent',\n inject: {\n isBlock: true,\n nodeProps: {\n nodeKey: 'textIndent',\n styleKey: 'textIndent',\n transformNodeValue({ getOptions, nodeValue }) {\n const { offset, unit } = getOptions();\n\n return nodeValue * offset! + unit!;\n },\n },\n targetPlugins: [BaseParagraphPlugin.key],\n },\n options: {\n offset: 24,\n unit: 'px',\n },\n});\n","import type {\n AnyObject,\n EditorNodesOptions,\n SlateEditor,\n} from '@udecode/plate';\n\nimport { BaseIndentPlugin } from '../BaseIndentPlugin';\n\nexport interface SetIndentOptions {\n /** GetNodeEntries options */\n getNodesOptions?: EditorNodesOptions;\n\n /**\n * 1 to indent -1 to outdent\n *\n * @default 1\n */\n offset?: number;\n\n /** Set other props than the indent one. These will be unset if indent = 0. */\n setNodesProps?: ({ indent }: { indent: number }) => AnyObject;\n\n /** Nodes props to unset when indent = 0. */\n unsetNodesProps?: string[];\n}\n\n/** Add offset to the indentation of the selected blocks. */\nexport const setIndent = (\n editor: SlateEditor,\n {\n getNodesOptions,\n offset = 1,\n setNodesProps,\n unsetNodesProps = [],\n }: SetIndentOptions\n) => {\n const { nodeKey } = editor.getInjectProps(BaseIndentPlugin);\n\n const _nodes = editor.api.nodes({\n block: true,\n mode: 'lowest',\n ...getNodesOptions,\n });\n const nodes = Array.from(_nodes);\n\n editor.tf.withoutNormalizing(() => {\n nodes.forEach(([node, path]) => {\n const blockIndent = (node[nodeKey!] as number) ?? 0;\n const newIndent = blockIndent + offset;\n\n const props = setNodesProps?.({ indent: newIndent }) ?? {};\n\n if (newIndent <= 0) {\n editor.tf.unsetNodes([nodeKey!, ...unsetNodesProps], {\n at: path,\n });\n } else {\n editor.tf.setNodes({ [nodeKey!]: newIndent, ...props }, { at: path });\n }\n });\n });\n};\n","import type { SlateEditor } from '@udecode/plate';\n\nimport { type SetIndentOptions, setIndent } from './setIndent';\n\n/** Increase the indentation of the selected blocks. */\nexport const indent = (editor: SlateEditor, options?: SetIndentOptions) => {\n setIndent(editor, { offset: 1, ...options });\n};\n","import type { SlateEditor } from '@udecode/plate';\n\nimport { type SetIndentOptions, setIndent } from './setIndent';\n\n/** Decrease the indentation of the selected blocks. */\nexport const outdent = (editor: SlateEditor, options?: SetIndentOptions) => {\n setIndent(editor, { offset: -1, ...options });\n};\n"],"mappings":";AAAA;AAAA,EAGE;AAAA,EACA;AAAA,OACK;;;ACHP,SAAS,sBAAsB;AAQxB,IAAM,aAA2C,CAAC;AAAA,EACvD;AAAA,EACA;AAAA,EACA;AAAA,EACA,IAAI,EAAE,cAAc;AACtB,MAAM;AACJ,SAAO;AAAA,IACL,YAAY;AAAA,MACV,cAAc,CAAC,MAAM,IAAI,GAAG;AAC1B,cAAM,EAAE,UAAU,IAAI,WAAW;AAEjC,cAAM,UAAU;AAChB,cAAM,EAAE,KAAK,IAAI;AAEjB,cAAM,QAAQ,eAAe,QAAQ,MAAM;AAE3C,YAAI,MAAM;AACR,cAAI,MAAM,SAAS,IAAI,GAAG;AACxB,gBAAI,aAAa,QAAQ,UAAU,QAAQ,SAAS,WAAW;AAC7D,qBAAO,GAAG,SAAS,EAAE,QAAQ,UAAU,GAAG,EAAE,IAAI,KAAK,CAAC;AAEtD;AAAA,YACF;AAAA,UACF,WAAW,QAAQ,QAAQ;AACzB,mBAAO,GAAG,WAAW,UAAU,EAAE,IAAI,KAAK,CAAC;AAE3C;AAAA,UACF;AAAA,QACF;AAEA,eAAO,cAAc,CAAC,MAAM,IAAI,CAAC;AA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