mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-04-06 02:37:14 +00:00
feat: better list input rule
This commit is contained in:
parent
dbe1c20c29
commit
a76baed599
@ -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",
|
||||
|
@ -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;
|
||||
|
@ -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 }),
|
||||
],
|
||||
|
Loading…
x
Reference in New Issue
Block a user