From 93dcd81bc663c8f64c7bc2ebb48ef8498cc42120 Mon Sep 17 00:00:00 2001 From: chris <kokosias@yahoo.gr> Date: Fri, 18 Mar 2022 10:48:38 +0200 Subject: [PATCH] math rules --- .../src/LinkService/LinkService.js | 3 + .../src/MathService/BlockInputRule.js | 2 + .../src/MathService/MathService.js | 14 ++-- .../src/RulesService/Rules.js | 67 ++++++++----------- 4 files changed, 39 insertions(+), 47 deletions(-) diff --git a/wax-prosemirror-services/src/LinkService/LinkService.js b/wax-prosemirror-services/src/LinkService/LinkService.js index df97efb14..fbd26d7bc 100644 --- a/wax-prosemirror-services/src/LinkService/LinkService.js +++ b/wax-prosemirror-services/src/LinkService/LinkService.js @@ -2,6 +2,7 @@ import { LinkComponent } from 'wax-prosemirror-components'; import { linkMark } from 'wax-prosemirror-schema'; import Service from '../Service'; import LinkTool from './LinkTool'; +import linkRule from './LinkInputRule'; export default class LinkService extends Service { name = 'LinkService'; @@ -21,6 +22,7 @@ export default class LinkService extends Service { register() { this.container.bind('Link').to(LinkTool); + const createRule = this.container.get('CreateRule'); const createMark = this.container.get('CreateMark'); createMark( { @@ -28,5 +30,6 @@ export default class LinkService extends Service { }, { toWaxSchema: true }, ); + createRule([linkRule(this.schema.marks.link)]); } } diff --git a/wax-prosemirror-services/src/MathService/BlockInputRule.js b/wax-prosemirror-services/src/MathService/BlockInputRule.js index 1b13412fb..adf636deb 100644 --- a/wax-prosemirror-services/src/MathService/BlockInputRule.js +++ b/wax-prosemirror-services/src/MathService/BlockInputRule.js @@ -6,12 +6,14 @@ const blockInputRule = (pattern, nodeType, getAttrs) => { return new InputRule(pattern, (state, match, start, end) => { let $start = state.doc.resolve(start); let attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs; + console.log('kjhkjh', $start, nodeType); if ( !$start .node(-1) .canReplaceWith($start.index(-1), $start.indexAfter(-1), nodeType) ) return null; + let tr = state.tr .delete(start, end) .setBlockType(start, start, nodeType, attrs); diff --git a/wax-prosemirror-services/src/MathService/MathService.js b/wax-prosemirror-services/src/MathService/MathService.js index 8525d4207..f12c7994a 100644 --- a/wax-prosemirror-services/src/MathService/MathService.js +++ b/wax-prosemirror-services/src/MathService/MathService.js @@ -15,19 +15,13 @@ class MathService extends Service { boot() { this.app.PmPlugins.add('mathplugin', mathPlugin); this.app.PmPlugins.add('mathselectplugin', mathSelectPlugin); - - const schema = this.container.get('Schema'); - const rules = this.container.get('Rules'); - const newRules = [ - inlineInputRule(/(?!\\)\$(.+)(?!\\)\$/, schema.schema.nodes.math_inline), - blockInputRule(/^\$\$\s+$/, schema.schema.nodes.math_display), - ]; - // rules.addRule(newRules); } register() { const createNode = this.container.get('CreateNode'); const createMark = this.container.get('CreateMark'); + const createRule = this.container.get('CreateRule'); + createNode({ math_display: mathDisplayNode, }); @@ -37,6 +31,10 @@ class MathService extends Service { createMark({ math_select: mathSelectMark, }); + createRule([ + blockInputRule(/^\$\$\s+$/, this.schema.nodes.math_display), + inlineInputRule(/(?!\\)\$(.+)(?!\\)\$/, this.schema.nodes.math_inline), + ]); } } diff --git a/wax-prosemirror-services/src/RulesService/Rules.js b/wax-prosemirror-services/src/RulesService/Rules.js index e105e9a71..b682ddb74 100644 --- a/wax-prosemirror-services/src/RulesService/Rules.js +++ b/wax-prosemirror-services/src/RulesService/Rules.js @@ -6,56 +6,45 @@ import { smartQuotes, } from 'prosemirror-inputrules'; -// TODO add through service. -import inlineInputRule from '../MathService/InlineInputRule'; -import blockInputRule from '../MathService/BlockInputRule'; -import linkRule from '../LinkService/LinkInputRule'; +const defaultRules = [ + ...smartQuotes, + // > blockquote + // wrappingInputRule(/^\s*>\s$/, this.schema.nodes.blockquote), + + // // 1. ordered list + // wrappingInputRule( + // /^(\d+)\.\s$/, + // this.schema.nodes.orderedlist, + // match => ({ order: +match[1] }), + // (match, node) => node.childCount + node.attrs.order === +match[1], + // ), + + // // * bullet list + // wrappingInputRule(/^\s*([-+*])\s$/, this.schema.nodes.bulletlist), + + // // ``` code block + // // textblockTypeInputRule(/^```$/, this.schema.nodes.code_block), + + // // # heading + // textblockTypeInputRule( + // new RegExp('^(#{1,6})\\s$'), + // this.schema.nodes.heading, + // match => ({ level: match[1].length }), + // ), +]; @injectable() class Rules { - extendedRules = this.allRules(); + extendedRules = defaultRules; addRule(rule) { this.extendedRules.push(...rule); - // this.extendedRules = this.allRules().concat(...rules); + // this.extendedRules = defaultRules.concat(...rule); } createRules() { const rulesCreated = inputRules({ rules: this.extendedRules }); - console.log(rulesCreated); return rulesCreated; } - - allRules() { - return [ - ...smartQuotes, - // > blockquote - // linkRule(this.schema.marks.link), - // wrappingInputRule(/^\s*>\s$/, this.schema.nodes.blockquote), - - // // 1. ordered list - // wrappingInputRule( - // /^(\d+)\.\s$/, - // this.schema.nodes.orderedlist, - // match => ({ order: +match[1] }), - // (match, node) => node.childCount + node.attrs.order === +match[1], - // ), - - // // * bullet list - // wrappingInputRule(/^\s*([-+*])\s$/, this.schema.nodes.bulletlist), - - // // ``` code block - // // textblockTypeInputRule(/^```$/, this.schema.nodes.code_block), - - // // # heading - // textblockTypeInputRule( - // new RegExp('^(#{1,6})\\s$'), - // this.schema.nodes.heading, - // match => ({ level: match[1].length }), - // ), - // inlineInputRule(/(?!\\)\$(.+)(?!\\)\$/, this.schema.nodes.math_inline), - // blockInputRule(/^\$\$\s+$/, this.schema.nodes.math_display), - ]; - } } export default Rules; -- GitLab