(editor: T, options: AlignOptions = {})
| 13 | } |
| 14 | |
| 15 | export const withAlign = <T extends Editable>(editor: T, options: AlignOptions = {}) => { |
| 16 | const newEditor = editor as T & AlignEditor |
| 17 | |
| 18 | setOptions(newEditor, options) |
| 19 | |
| 20 | newEditor.toggleAlign = (value = AlignValue.Left) => { |
| 21 | editor.normalizeSelection(selection => { |
| 22 | if (!selection) return |
| 23 | if (editor.selection !== selection) editor.selection = selection |
| 24 | if (!AlignEditor.isAlignEditor(editor)) return |
| 25 | |
| 26 | const lowestBlocks = Editor.nodes<Element>(editor, { |
| 27 | mode: 'lowest', |
| 28 | match: n => Editor.isBlock(editor, n), |
| 29 | }) |
| 30 | |
| 31 | for (const [element, path] of lowestBlocks) { |
| 32 | const entry = Editor.above<List>(editor, { |
| 33 | at: path, |
| 34 | match: n => editor.isList(n), |
| 35 | }) |
| 36 | const el = entry ? entry[0] : element |
| 37 | if (Align.isAlign(el) && el[ALIGN_ATTR_KEY] === value) continue |
| 38 | const at = entry ? entry[1] : path |
| 39 | Transforms.setNodes<Align>( |
| 40 | editor, |
| 41 | { |
| 42 | [ALIGN_ATTR_KEY]: value, |
| 43 | }, |
| 44 | { |
| 45 | at, |
| 46 | }, |
| 47 | ) |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | const { renderElementAttributes } = newEditor |
| 53 | |
| 54 | newEditor.renderElementAttributes = ({ attributes, element }) => { |
| 55 | const style: typeof attributes.style = attributes.style ?? {} |
| 56 | if (Align.isAlign(element)) { |
| 57 | const textAlign = element[ALIGN_ATTR_KEY] |
| 58 | const isList = editor.isList(element) |
| 59 | const attr = isList && textAlign !== AlignValue.Justify ? 'justifyContent' : 'textAlign' |
| 60 | if (textAlign === AlignValue.Left) { |
| 61 | delete style[attr] |
| 62 | } else { |
| 63 | style[attr] = textAlign |
| 64 | } |
| 65 | attributes = Object.assign({}, attributes, { style }) |
| 66 | } |
| 67 | return renderElementAttributes({ |
| 68 | attributes, |
| 69 | element, |
| 70 | }) |
| 71 | } |
| 72 |
no test coverage detected
searching dependent graphs…