From e73dffad0bb5e081232c9626801b83f38f75c3c6 Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 6 Jun 2020 12:56:24 +0200 Subject: [PATCH] parse incomplete attrs --- spec-es6/attribute_parser.spec.js | 6 +++ src/public/app/services/attribute_parser.js | 44 +++++++++++++-------- src/public/app/widgets/note_attributes.js | 12 +++++- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/spec-es6/attribute_parser.spec.js b/spec-es6/attribute_parser.spec.js index 6a878189c..899969fc3 100644 --- a/spec-es6/attribute_parser.spec.js +++ b/spec-es6/attribute_parser.spec.js @@ -12,6 +12,12 @@ describe("Lexer", () => { .toEqual(["#label", "=", "Hallo"]); }); + it("label with value", () => { + const tokens = attributeParser.lexer("#label=Hallo"); + expect(tokens[0].startIndex).toEqual(0); + expect(tokens[0].endIndex).toEqual(5); + }); + it("relation with value", () => { expect(attributeParser.lexer('~relation=note').map(t => t.text)) .toEqual(["~relation", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"]); diff --git a/src/public/app/services/attribute_parser.js b/src/public/app/services/attribute_parser.js index 4c4388c04..774d17f56 100644 --- a/src/public/app/services/attribute_parser.js +++ b/src/public/app/services/attribute_parser.js @@ -43,7 +43,7 @@ function lexer(str) { tokens.push({ text: currentWord, - startIndex: endIndex - currentWord.length, + startIndex: endIndex - currentWord.length + 1, endIndex: endIndex }); @@ -110,7 +110,7 @@ function lexer(str) { return tokens; } -function parser(tokens) { +function parser(tokens, allowEmptyRelations = false) { const attrs = []; for (let i = 0; i < tokens.length; i++) { @@ -121,8 +121,8 @@ function parser(tokens) { type: 'label', name: text.substr(1), isInheritable: false, // FIXME - startIndex, - endIndex + nameStartIndex: startIndex, + nameEndIndex: endIndex }; if (i + 1 < tokens.length && tokens[i + 1].text === "=") { @@ -133,13 +133,30 @@ function parser(tokens) { i += 2; attr.value = tokens[i].text; + attr.valueStartIndex = tokens[i].startIndex; + attr.valueEndIndex = tokens[i].endIndex; } attrs.push(attr); } else if (text.startsWith('~')) { + const attr = { + type: 'relation', + name: text.substr(1), + isInheritable: false, // FIXME + nameStartIndex: startIndex, + nameEndIndex: endIndex + }; + + attrs.push(attr); + if (i + 2 >= tokens.length || tokens[i + 1].text !== '=') { - throw new Error(`Relation "${text}" should point to a note.`); + if (allowEmptyRelations) { + break; + } + else { + throw new Error(`Relation "${text}" should point to a note.`); + } } i += 2; @@ -151,16 +168,9 @@ function parser(tokens) { const noteId = notePath.split('/').pop(); - const attr = { - type: 'relation', - name: text.substr(1), - isInheritable: false, // FIXME - value: noteId, - startIndex, - endIndex - }; - - attrs.push(attr); + attr.value = noteId; + attr.valueStartIndex = tokens[i].startIndex; + attr.valueEndIndex = tokens[i].endIndex; } else { throw new Error(`Unrecognized attribute "${text}"`); @@ -170,10 +180,10 @@ function parser(tokens) { return attrs; } -function lexAndParse(str) { +function lexAndParse(str, allowEmptyRelations = false) { const tokens = lexer(str); - return parser(tokens); + return parser(tokens, allowEmptyRelations); } export default { diff --git a/src/public/app/widgets/note_attributes.js b/src/public/app/widgets/note_attributes.js index a0874125b..328529bbd 100644 --- a/src/public/app/widgets/note_attributes.js +++ b/src/public/app/widgets/note_attributes.js @@ -116,7 +116,17 @@ export default class NoteAttributesWidget extends TabAwareWidget { this.$editor.on("click", () => { const pos = this.textEditor.model.document.selection.getFirstPosition(); - console.log(pos.textNode && pos.textNode.data, pos.parent.textNode && pos.parent.textNode.data, pos.offset); + + if (pos && pos.textNode && pos.textNode.data) { + const attr = pos.textNode.data; + const index = pos.offset - pos.textNode.startOffset; + + const attrs = attributesParser.lexAndParse(attr, true); + + console.log(attrs); + + console.log(attr.substr(0, index) + '|' + attr.substr(index)); + } }); this.textEditor = await BalloonEditor.create(this.$editor[0], {