|
| 1 | +/* This Source Code has been derived from Tiptiz. |
| 2 | + * https://github.com/tiptiz/editor/blob/main/packages/tiptiz-extension-indent/src/indent.ts |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + * Copyright © 2024 Owen Kriz. |
| 5 | + * Modifications are licensed under the MIT License. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { Dispatch } from '@tiptap/core'; |
| 9 | +import type { EditorState, Transaction } from '@tiptap/pm/state'; |
| 10 | + |
| 11 | +import { Extension } from '@tiptap/core'; |
| 12 | +import { AllSelection, TextSelection } from '@tiptap/pm/state'; |
| 13 | + |
| 14 | +export interface TextIndentOptions { |
| 15 | + minLevel: number; |
| 16 | + maxLevel: number; |
| 17 | + types: Array<string>; |
| 18 | +} |
| 19 | + |
| 20 | +export const TextIndent = Extension.create<TextIndentOptions>({ |
| 21 | + name: 'textIndent', |
| 22 | + |
| 23 | + addOptions() { |
| 24 | + return { |
| 25 | + minLevel: 0, |
| 26 | + maxLevel: 5, |
| 27 | + types: ['heading', 'paragraph', 'listItem', 'taskItem'], |
| 28 | + }; |
| 29 | + }, |
| 30 | + |
| 31 | + addGlobalAttributes() { |
| 32 | + return [ |
| 33 | + { |
| 34 | + types: this.options.types, |
| 35 | + attributes: { |
| 36 | + indent: { |
| 37 | + default: null, |
| 38 | + parseHTML: (element) => { |
| 39 | + const minLevel = this.options.minLevel; |
| 40 | + const maxLevel = this.options.maxLevel; |
| 41 | + const indent = element.style.textIndent; |
| 42 | + return indent ? Math.max(minLevel, Math.min(maxLevel, parseInt(indent, 10))) : null; |
| 43 | + }, |
| 44 | + renderHTML: (attributes) => { |
| 45 | + if (!attributes.indent) return {}; |
| 46 | + return { |
| 47 | + style: `text-indent: ${attributes.indent}rem;`, |
| 48 | + }; |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + ]; |
| 54 | + }, |
| 55 | + |
| 56 | + addCommands() { |
| 57 | + const updateNodeIndentMarkup = (tr: Transaction, pos: number, delta: number) => { |
| 58 | + const node = tr.doc.nodeAt(pos); |
| 59 | + if (!node) return tr; |
| 60 | + |
| 61 | + const minLevel = this.options.minLevel; |
| 62 | + const maxLevel = this.options.maxLevel; |
| 63 | + |
| 64 | + let level = (node.attrs.indent || 0) + delta; |
| 65 | + level = Math.max(minLevel, Math.min(maxLevel, parseInt(level, 10))); |
| 66 | + |
| 67 | + if (level === node.attrs.indent) return tr; |
| 68 | + |
| 69 | + return tr.setNodeMarkup(pos, node.type, { ...node.attrs, indent: level }, node.marks); |
| 70 | + }; |
| 71 | + |
| 72 | + const updateIndentLevel = (tr: Transaction, delta: number) => { |
| 73 | + if (tr.selection instanceof TextSelection || tr.selection instanceof AllSelection) { |
| 74 | + const { from, to } = tr.selection; |
| 75 | + tr.doc.nodesBetween(from, to, (node, pos) => { |
| 76 | + if (this.options.types.includes(node.type.name)) { |
| 77 | + tr = updateNodeIndentMarkup(tr, pos, delta); |
| 78 | + return false; |
| 79 | + } |
| 80 | + return true; |
| 81 | + }); |
| 82 | + } |
| 83 | + return tr; |
| 84 | + }; |
| 85 | + |
| 86 | + type CommanderArgs = { |
| 87 | + tr: Transaction; |
| 88 | + state: EditorState; |
| 89 | + dispatch: Dispatch; |
| 90 | + }; |
| 91 | + |
| 92 | + const commanderFactory = (direction: number) => () => |
| 93 | + function chainHandler({ tr, state, dispatch }: CommanderArgs) { |
| 94 | + const { selection } = state; |
| 95 | + tr.setSelection(selection); |
| 96 | + tr = updateIndentLevel(tr, direction); |
| 97 | + if (tr.docChanged) { |
| 98 | + if (dispatch instanceof Function) dispatch(tr); |
| 99 | + return true; |
| 100 | + } |
| 101 | + return false; |
| 102 | + }; |
| 103 | + |
| 104 | + return { |
| 105 | + textIndent: commanderFactory(1), |
| 106 | + textOutdent: commanderFactory(-1), |
| 107 | + }; |
| 108 | + }, |
| 109 | +}); |
| 110 | + |
| 111 | +declare module '@tiptap/core' { |
| 112 | + interface Commands<ReturnType> { |
| 113 | + textIndent: { |
| 114 | + textIndent: () => ReturnType; |
| 115 | + textOutdent: () => ReturnType; |
| 116 | + }; |
| 117 | + } |
| 118 | +} |
0 commit comments