diff options
author | Bob Zeleznik <zzzman@gmail.com> | 2019-09-03 23:07:42 -0400 |
---|---|---|
committer | Bob Zeleznik <zzzman@gmail.com> | 2019-09-03 23:07:42 -0400 |
commit | 155cf5c5a1b45aafe7a5632f2ee6ecf957f04dde (patch) | |
tree | 1ddf0986b067944a1ab07925768722bafd746c28 /src | |
parent | 4921ef0121818381e4139137c0d204d41cb64a20 (diff) |
fixes for bullets
Diffstat (limited to 'src')
-rw-r--r-- | src/client/util/ProsemirrorExampleTransfer.ts | 59 | ||||
-rw-r--r-- | src/client/util/RichTextSchema.tsx | 11 |
2 files changed, 48 insertions, 22 deletions
diff --git a/src/client/util/ProsemirrorExampleTransfer.ts b/src/client/util/ProsemirrorExampleTransfer.ts index 419311df8..5016e72c6 100644 --- a/src/client/util/ProsemirrorExampleTransfer.ts +++ b/src/client/util/ProsemirrorExampleTransfer.ts @@ -93,35 +93,63 @@ export default function buildKeymap<S extends Schema<any>>(schema: S, mapKeys?: bind("Mod-s", TooltipTextMenu.insertStar); + let updateOrderedList = (start: number, rangeStart: any, delta: number, tx2: Transaction, forward: boolean) => { + let bs = rangeStart.attrs.bulletStyle; + bs + delta > 0 && tx2.setNodeMarkup(start, rangeStart.type, { mapStyle: rangeStart.attrs.mapStyle, bulletStyle: rangeStart.attrs.bulletStyle + delta }, rangeStart.marks); + + let brk = false; + rangeStart.descendants((node: any, offset: any, index: any) => { + if (node.type === schema.nodes.ordered_list || node.type === schema.nodes.list_item) { + if (!brk && (bs !== node.attrs.bulletStyle || delta > 0 || (forward && bs > 1))) { + tx2.setNodeMarkup(start + offset + 1, node.type, { mapStyle: node.attrs.mapStyle, bulletStyle: node.attrs.bulletStyle + delta }, node.marks); + } else { + brk = true; + } + } + }); + } + let updateBullets = (tx2: Transaction, refStart: number, delta: number) => { - for (let i = refStart; i > 0; i--) { + let i = refStart; + for (let i = refStart; i >= 0; i--) { let testPos = tx2.doc.nodeAt(i); - if (testPos && testPos.type === schema.nodes.list_item) { + if (!testPos) { + for (let i = refStart + 1; i <= tx2.doc.nodeSize; i++) { + try { + let testPos = tx2.doc.nodeAt(i); + if (testPos && testPos.type === schema.nodes.ordered_list) { + updateOrderedList(i, testPos, delta, tx2, true); + break; + } + } catch (e) { + break; + } + } + break; + } + if ((testPos.type === schema.nodes.list_item || testPos.type === schema.nodes.ordered_list)) { let start = i; let preve = i > 0 && tx2.doc.nodeAt(start - 1); if (preve && preve.type === schema.nodes.ordered_list) { start = start - 1; } let rangeStart = tx2.doc.nodeAt(start); - if (rangeStart && rangeStart.type === schema.nodes.ordered_list) { - tx2.setNodeMarkup(start, rangeStart.type, { ...rangeStart.attrs, bulletStyle: rangeStart.attrs.bulletStyle + delta }, rangeStart.marks); + if (rangeStart) { + updateOrderedList(start, rangeStart, delta, tx2, false); } - rangeStart && rangeStart.descendants((node: any, offset: any, index: any) => { - if (node.type === schema.nodes.ordered_list) { - tx2.setNodeMarkup(start + offset + 1, node.type, { ...node.attrs, bulletStyle: node.attrs.bulletStyle + delta }, node.marks); - } - }); break; } } } + bind("Tab", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => { var ref = state.selection; var range = ref.$from.blockRange(ref.$to); var marks = state.storedMarks || (state.selection.$to.parentOffset && state.selection.$from.marks()); if (!sinkListItem(schema.nodes.list_item)(state, (tx2: Transaction) => { - updateBullets(tx2, range!.start, 1); + var range = state.selection.$from.blockRange(state.selection.$to); + updateBullets(tx2, range!.start - 1, 1); marks && tx2.ensureMarks([...marks]); marks && tx2.setStoredMarks([...marks]); dispatch(tx2); @@ -129,13 +157,7 @@ export default function buildKeymap<S extends Schema<any>>(schema: S, mapKeys?: let sxf = state.tr.setSelection(TextSelection.create(state.doc, range!.start, range!.end)); let newstate = state.applyTransaction(sxf); if (!wrapInList(schema.nodes.ordered_list)(newstate.state, (tx2: Transaction) => { - for (let i = range!.start; i >= 0; i--) { - let rangeStart = tx2.doc.nodeAt(i); - if (rangeStart && rangeStart.type === schema.nodes.ordered_list) { - tx2.setNodeMarkup(i, rangeStart.type, { ...rangeStart.attrs, bulletStyle: 1 }, rangeStart.marks); - break; - } - } + updateBullets(tx2, range!.start, 1); // when promoting to a list, assume list will format things so don't copy the stored marks. marks && tx2.ensureMarks([...marks]); marks && tx2.setStoredMarks([...marks]); @@ -151,9 +173,10 @@ export default function buildKeymap<S extends Schema<any>>(schema: S, mapKeys?: var marks = state.storedMarks || (state.selection.$to.parentOffset && state.selection.$from.marks()); let tr = state.tr; - updateBullets(tr, range!.start, -1); if (!liftListItem(schema.nodes.list_item)(tr, (tx2: Transaction) => { + var range = tx2.selection.$from.blockRange(tx2.selection.$to); + updateBullets(tx2, range!.start, -1); marks && tx2.ensureMarks([...marks]); marks && tx2.setStoredMarks([...marks]); dispatch(tx2); diff --git a/src/client/util/RichTextSchema.tsx b/src/client/util/RichTextSchema.tsx index 25d972857..75e982872 100644 --- a/src/client/util/RichTextSchema.tsx +++ b/src/client/util/RichTextSchema.tsx @@ -203,7 +203,6 @@ export const nodes: { [index: string]: NodeSpec } = { const decMap = bs ? "decimal" + bs : ""; const multiMap = bs === 1 ? "decimal1" : bs === 2 ? "upper-alpha" : bs === 3 ? "lower-roman" : bs === 4 ? "lower-alpha" : ""; let map = node.attrs.mapStyle === "decimal" ? decMap : multiMap; - for (let i = 0; i < node.childCount; i++) node.child(i).attrs.className = map; return ['ol', { class: `${map}-ol`, style: `list-style: none;` }, 0]; //return node.attrs.bulletStyle < 2 ? ['ol', { class: `${map}-ol`, style: `list-style: none;` }, 0] : // ['ol', { class: `${node.attrs.bulletStyle}`, style: `list-style: ${node.attrs.bulletStyle}; font-size: 5px` }, "hello"]; @@ -216,7 +215,6 @@ export const nodes: { [index: string]: NodeSpec } = { group: 'block', // parseDOM: [{ tag: "ul" }, { style: 'list-style-type=disc' }], toDOM(node: Node<any>) { - for (let i = 0; i < node.childCount; i++) node.child(i).attrs.className = ""; return ['ul', 0]; } }, @@ -231,12 +229,17 @@ export const nodes: { [index: string]: NodeSpec } = { // }, list_item: { attrs: { - className: { default: "" } + bulletStyle: { default: 0 }, + mapStyle: { default: "decimal" }, }, ...listItem, content: 'paragraph block*', toDOM(node: any) { - return ["li", { class: node.attrs.className }, 0]; + const bs = node.attrs.bulletStyle; + const decMap = bs ? "decimal" + bs : ""; + const multiMap = bs === 1 ? "decimal1" : bs === 2 ? "upper-alpha" : bs === 3 ? "lower-roman" : bs === 4 ? "lower-alpha" : ""; + let map = node.attrs.mapStyle === "decimal" ? decMap : multiMap; + return ["li", { class: `${map}` }, 0]; } }, }; |