diff --git a/plugins/tiddlywiki/prosemirror/ast/from-prosemirror.js b/plugins/tiddlywiki/prosemirror/ast/from-prosemirror.js index 3521a2e0c..12d24fbad 100644 --- a/plugins/tiddlywiki/prosemirror/ast/from-prosemirror.js +++ b/plugins/tiddlywiki/prosemirror/ast/from-prosemirror.js @@ -46,7 +46,7 @@ function list(builder, node, context) { let listItems = []; // Add content from current node to list items - node.content.forEach(item => { + node.content?.forEach?.(item => { listItems.push({ type: "element", tag: "li", @@ -66,7 +66,7 @@ function list(builder, node, context) { const consumedNode = context.nodes.shift(); // Merge its content into current list - consumedNode.content.forEach(item => { + consumedNode.content?.forEach?.(item => { listItems.push({ type: "element", tag: "li", diff --git a/plugins/tiddlywiki/prosemirror/setup/inputrules.js b/plugins/tiddlywiki/prosemirror/setup/inputrules.js index 425af3c51..b1078136a 100644 --- a/plugins/tiddlywiki/prosemirror/setup/inputrules.js +++ b/plugins/tiddlywiki/prosemirror/setup/inputrules.js @@ -8,41 +8,40 @@ module-type: library "use strict"; var { inputRules, wrappingInputRule, textblockTypeInputRule, smartQuotes, emDash, ellipsis } = require("prosemirror-inputrules"); +var { wrappingListInputRule } = require("prosemirror-flat-list"); var { NodeType, Schema } = require("prosemirror-model"); function blockQuoteRule(nodeType) { return wrappingInputRule(/^\s*>\s$/, nodeType); } -function orderedListRule(nodeType) { - return wrappingInputRule( - /^(\d+)\.\s$/, - nodeType, - function(match) { return { order: +match[1] }; }, - function(match, node) { return node.childCount + node.attrs.order == +match[1]; } - ); -} - -function bulletListRule(nodeType) { - return wrappingInputRule(/^\s*([-+*])\s$/, nodeType); -} - function codeBlockRule(nodeType) { return textblockTypeInputRule(/^```$/, nodeType); } function headingRule(nodeType, maxLevel) { - return textblockTypeInputRule(new RegExp("^(#{1," + maxLevel + "})\\s$"), nodeType, function(match) { return { level: match[1].length }; }); + return textblockTypeInputRule(new RegExp("^(\\!{1," + maxLevel + "}|\!{1," + maxLevel + "})\\s$"), nodeType, function(match) { return { level: match[1].length }; }); } function buildInputRules(schema) { var rules = smartQuotes.concat(ellipsis, emDash), type; if (type = schema.nodes.blockquote) rules.push(blockQuoteRule(type)); - if (type = schema.nodes.ordered_list) rules.push(orderedListRule(type)); - if (type = schema.nodes.bullet_list) rules.push(bulletListRule(type)); + if (type = schema.nodes.list) { + rules.push(wrappingListInputRule(/^\s?([*-])\s$/, { + kind: 'bullet', + collapsed: false, + })); + rules.push(wrappingListInputRule(/^\s?(#)\s$|^\s?(\d+)\.\s$/, ({ match }) => { + const order = match[1] === "#" ? 1 : parseInteger(match[1]); + return { + kind: 'ordered', + collapsed: false, + order: order != null && order >= 2 ? order : null, + }; + })); + } if (type = schema.nodes.code_block) rules.push(codeBlockRule(type)); if (type = schema.nodes.heading) rules.push(headingRule(type, 6)); return inputRules({ rules: rules }); } - exports.buildInputRules = buildInputRules; diff --git a/plugins/tiddlywiki/prosemirror/widget.js b/plugins/tiddlywiki/prosemirror/widget.js index 3edd8a560..96eb542ae 100644 --- a/plugins/tiddlywiki/prosemirror/widget.js +++ b/plugins/tiddlywiki/prosemirror/widget.js @@ -19,7 +19,6 @@ var { schema: basicSchema } = require("prosemirror-schema-basic"); var { createListPlugins, createListSpec, - listInputRules, listKeymap } = require("prosemirror-flat-list"); var { exampleSetup } = require("$:/plugins/tiddlywiki/prosemirror/setup/setup.js"); @@ -53,16 +52,15 @@ ProsemirrorWidget.prototype.render = function(parent,nextSibling) { console.log(`initial doc`, doc); var container = $tw.utils.domMaker('div', { - class: 'tc-prosemirror-container', + class: 'tc-prosemirror-container', }); var schema = new Schema({ - nodes: basicSchema.spec.nodes.append({ list: createListSpec() }), - marks: basicSchema.spec.marks, + nodes: basicSchema.spec.nodes.append({ list: createListSpec() }), + marks: basicSchema.spec.marks, }) var listKeymapPlugin = keymap(listKeymap) - var listInputRulePlugin = inputRules({ rules: listInputRules }) var listPlugins = createListPlugins({ schema }) var self = this; @@ -72,7 +70,6 @@ ProsemirrorWidget.prototype.render = function(parent,nextSibling) { doc: schema.nodeFromJSON(doc), plugins: [ listKeymapPlugin, - listInputRulePlugin, ...listPlugins, ...exampleSetup({ schema }), ],