|
1 | | -import {Plugin, PluginKey} from "prosemirror-state"; |
| 1 | +import { Plugin, PluginKey } from "prosemirror-state"; |
2 | 2 |
|
3 | 3 | const PLUGIN_KEY = new PluginKey(`ordered-list`); |
4 | 4 |
|
5 | 5 | export const OrderedListPlugin = () => { |
6 | 6 | return new Plugin({ |
7 | 7 | key: PLUGIN_KEY, |
8 | 8 | appendTransaction: (_transactions, _oldState, newState) => { |
9 | | - const newTr = newState.tr |
10 | | - let modified = false |
11 | | - let count = 1 |
12 | | - let skip = 0 |
| 9 | + const newTr = newState.tr; |
| 10 | + let modified = false; |
| 11 | + let count = 1; |
| 12 | + let skip = 0; |
13 | 13 | newState.doc.descendants((node, pos) => { |
14 | | - if (node.type.name === "tcblock" && !node.attrs.listType) count = 1 |
15 | | - if (skip == 0 && node.type.name === "tcblock" && node.attrs.listType === "oli") { |
16 | | - skip = node.content.childCount |
| 14 | + if (node.type.name === "tcblock" && !node.attrs.listType) count = 1; |
| 15 | + if ( |
| 16 | + skip === 0 && |
| 17 | + node.type.name === "tcblock" && |
| 18 | + node.attrs.listType === "oli" |
| 19 | + ) { |
| 20 | + skip = node.content.childCount; |
17 | 21 | // This assumes that the content node is always the first child of the oli block, |
18 | 22 | // as the content model grows this assumption may need to change |
19 | 23 | if (node.content.child(0).attrs.position !== `${count}.`) { |
20 | 24 | // TODO: @DAlperin currently sub-items just continue from the order of the parent, |
21 | 25 | // sub-items should be ordered separately with letters or roman numerals or some such |
22 | | - newTr.setNodeMarkup(pos+1, undefined, {...node.attrs, position: `${count}.`}) |
23 | | - modified = true |
| 26 | + newTr.setNodeMarkup(pos + 1, undefined, { |
| 27 | + ...node.attrs, |
| 28 | + position: `${count}.`, |
| 29 | + }); |
| 30 | + modified = true; |
24 | 31 | } |
25 | 32 |
|
26 | | - count++ |
27 | | - } else if (skip > 0) skip-- |
28 | | - }) |
29 | | - if (modified){ |
30 | | - return newTr |
| 33 | + count++; |
| 34 | + } else if (skip > 0) skip--; |
| 35 | + }); |
| 36 | + if (modified) { |
| 37 | + return newTr; |
31 | 38 | } |
32 | | - return null |
33 | | - } |
34 | | - }) |
35 | | -} |
| 39 | + return null; |
| 40 | + }, |
| 41 | + }); |
| 42 | +}; |
0 commit comments