Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions packages/core/src/extensions/Blocks/OrderedListPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
import {Plugin, PluginKey} from "prosemirror-state";
import { Plugin, PluginKey } from "prosemirror-state";

const PLUGIN_KEY = new PluginKey(`ordered-list`);

export const OrderedListPlugin = () => {
return new Plugin({
key: PLUGIN_KEY,
appendTransaction: (_transactions, _oldState, newState) => {
const newTr = newState.tr
let modified = false
let count = 1
let skip = 0
const newTr = newState.tr;
let modified = false;
let count = 1;
let skip = 0;
newState.doc.descendants((node, pos) => {
if (node.type.name === "tcblock" && !node.attrs.listType) count = 1
if (skip == 0 && node.type.name === "tcblock" && node.attrs.listType === "oli") {
skip = node.content.childCount
if (node.type.name === "tcblock" && !node.attrs.listType) {
count = 1;
}
if (
skip === 0 &&
node.type.name === "tcblock" &&
node.attrs.listType === "oli"
) {
skip = node.content.childCount;
// This assumes that the content node is always the first child of the oli block,
// as the content model grows this assumption may need to change
if (node.content.child(0).attrs.position !== `${count}.`) {
// TODO: @DAlperin currently sub-items just continue from the order of the parent,
// sub-items should be ordered separately with letters or roman numerals or some such
newTr.setNodeMarkup(pos+1, undefined, {...node.attrs, position: `${count}.`})
modified = true
newTr.setNodeMarkup(pos + 1, undefined, {
...node.attrs,
position: `${count}.`,
});
modified = true;
}

count++
} else if (skip > 0) skip--
})
if (modified){
return newTr
count++;
} else if (skip > 0) {
skip--;
}
});
if (modified) {
return newTr;
}
return null
}
})
}
return null;
},
});
};
2 changes: 1 addition & 1 deletion packages/core/src/extensions/Blocks/nodes/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Selection } from "prosemirror-state";
import styles from "./Block.module.css";
import { PreviousBlockTypePlugin } from "../PreviousBlockTypePlugin";
import { textblockTypeInputRuleSameNodeType } from "../rule";
import {OrderedListPlugin} from "../OrderedListPlugin";
import { OrderedListPlugin } from "../OrderedListPlugin";

export interface IBlock {
HTMLAttributes: Record<string, any>;
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/extensions/Blocks/nodes/Content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export const ContentBlock = Node.create<IBlock>({
default: undefined,
renderHTML: (attributes) => {
return {
"data-position": attributes.position
}
"data-position": attributes.position,
};
},
parseHTML: (element) => element.getAttribute("data-position")
}
}
parseHTML: (element) => element.getAttribute("data-position"),
},
};
},

content: "inline*",
Expand Down