From cac5951d0c2681245751e8fbf34d150015d66268 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Wed, 26 Jan 2022 14:19:14 -0800 Subject: [PATCH] Ignore empty nodes from serialization --- .../src/components/export/MDExporter.ts | 50 ++++----- .../src/components/export/TxtExporter.ts | 28 ++--- .../src/components/model/INodeModel.ts | 8 +- .../src/components/model/NoteModel.ts | 4 +- packages/mindplot/test/unit/export/Helper.ts | 24 ++--- .../test/unit/export/expected/complex.md | 1 - .../test/unit/export/expected/complex.txt | 102 +++++++----------- 7 files changed, 100 insertions(+), 117 deletions(-) diff --git a/packages/mindplot/src/components/export/MDExporter.ts b/packages/mindplot/src/components/export/MDExporter.ts index 486a4b0f..b402c7c6 100644 --- a/packages/mindplot/src/components/export/MDExporter.ts +++ b/packages/mindplot/src/components/export/MDExporter.ts @@ -24,7 +24,7 @@ import Exporter from './Exporter'; class MDExporter implements Exporter { private mindmap: Mindmap; - private footNotes = []; + private footNotes: string[] = []; constructor(mindmap: Mindmap) { this.mindmap = mindmap; @@ -63,32 +63,34 @@ class MDExporter implements Exporter { private traverseBranch(prefix: string, branches: Array) { let result = ''; - branches.forEach((node) => { - result = `${result}${prefix}- ${node.getText()}`; - node.getFeatures().forEach((f) => { - const type = f.getType(); - // Dump all features ... - if (type === 'link') { - result = `${result} ( [link](${(f as LinkModel).getUrl()}) )`; - } + branches + .filter((n) => n.getText() !== undefined) + .forEach((node) => { + result = `${result}${prefix}- ${node.getText()}`; + node.getFeatures().forEach((f) => { + const type = f.getType(); + // Dump all features ... + if (type === 'link') { + result = `${result} ( [link](${(f as LinkModel).getUrl()}) )`; + } - if (type === 'note') { - const note = f as NoteModel; - this.footNotes.push(note.getText()); - result = `${result}[^${this.footNotes.length}] `; - } + if (type === 'note') { + const note = f as NoteModel; + this.footNotes.push(note.getText()); + result = `${result}[^${this.footNotes.length}] `; + } - // if(type === 'icon'){ - // const icon = f as IconModel; - // result = result + ` ![${icon.getIconType().replace('_','')}!](https://app.wisemapping.com/images/${icon.getIconType()}.svg )` - // } + // if(type === 'icon'){ + // const icon = f as IconModel; + // result = result + ` ![${icon.getIconType().replace('_','')}!](https://app.wisemapping.com/images/${icon.getIconType()}.svg )` + // } + }); + result = `${result}\n`; + + if (node.getChildren().filter((n) => n.getText() !== undefined).length > 0) { + result += this.traverseBranch(`${prefix}\t`, node.getChildren()); + } }); - result = `${result}\n`; - - if (node.getChildren().length > 0) { - result += this.traverseBranch(`${prefix}\t`, node.getChildren()); - } - }); return result; } } diff --git a/packages/mindplot/src/components/export/TxtExporter.ts b/packages/mindplot/src/components/export/TxtExporter.ts index 97986614..7b7cd307 100644 --- a/packages/mindplot/src/components/export/TxtExporter.ts +++ b/packages/mindplot/src/components/export/TxtExporter.ts @@ -39,22 +39,24 @@ class TxtExporter implements Exporter { return Promise.resolve(retult); } - private traverseBranch(prefix: string, branches: Array) { + private traverseBranch(prefix: string, branches: INodeModel[]) { let result = ''; - branches.forEach((node, index) => { - result = `${result}${prefix}${index + 1} ${node.getText()}`; - node.getFeatures().forEach((f) => { - const type = f.getType(); - if (type === 'link') { - result = `${result} [link: ${(f as LinkModel).getUrl()}]`; + branches + .filter((n) => n.getText() !== undefined) + .forEach((node, index) => { + result = `${result}${prefix}${index + 1} ${node.getText()}`; + node.getFeatures().forEach((f) => { + const type = f.getType(); + if (type === 'link') { + result = `${result} [link: ${(f as LinkModel).getUrl()}]`; + } + }); + result = `${result}\n`; + + if (node.getChildren().filter((n) => n.getText() !== undefined).length > 0) { + result += this.traverseBranch(`\t${prefix}${index + 1}.`, node.getChildren()); } }); - result = `${result}\n`; - - if (node.getChildren().length > 0) { - result += this.traverseBranch(`\t${prefix}${index + 1}.`, node.getChildren()); - } - }); return result; } } diff --git a/packages/mindplot/src/components/model/INodeModel.ts b/packages/mindplot/src/components/model/INodeModel.ts index 91d2367e..3f379781 100644 --- a/packages/mindplot/src/components/model/INodeModel.ts +++ b/packages/mindplot/src/components/model/INodeModel.ts @@ -68,7 +68,7 @@ abstract class INodeModel { this.putProperty('text', text); } - getText(): string { + getText(): string | undefined { return this.getProperty('text') as string; } @@ -89,7 +89,7 @@ abstract class INodeModel { this.putProperty('imageSize', `{width:${width},height:${height}}`); } - getImageSize(): {width: number, height: number} { + getImageSize(): { width: number, height: number } { const value = this.getProperty('imageSize') as string; let result; if (value != null) { @@ -260,7 +260,7 @@ abstract class INodeModel { const tmindmap = target.getMindmap(); children.forEach((snode) => { - const tnode:INodeModel = tmindmap.createNode(snode.getType(), snode.getId()); + const tnode: INodeModel = tmindmap.createNode(snode.getType(), snode.getId()); snode.copyTo(tnode); target.append(tnode); }); @@ -289,7 +289,7 @@ abstract class INodeModel { abstract getProperty(key: string): number | string | boolean; - abstract putProperty(key: string, value: number | string| boolean): void; + abstract putProperty(key: string, value: number | string | boolean): void; abstract setParent(parent: INodeModel): void; diff --git a/packages/mindplot/src/components/model/NoteModel.ts b/packages/mindplot/src/components/model/NoteModel.ts index 745a95a4..7b90876f 100644 --- a/packages/mindplot/src/components/model/NoteModel.ts +++ b/packages/mindplot/src/components/model/NoteModel.ts @@ -26,12 +26,12 @@ class NoteModel extends FeatureModel { } /** */ - getText():string { + getText(): string { return this.getAttribute('text') as string; } /** */ - setText(text:string) { + setText(text: string) { $assert(text, 'text can not be null'); this.setAttribute('text', text); } diff --git a/packages/mindplot/test/unit/export/Helper.ts b/packages/mindplot/test/unit/export/Helper.ts index cccbd2b8..244058d7 100644 --- a/packages/mindplot/test/unit/export/Helper.ts +++ b/packages/mindplot/test/unit/export/Helper.ts @@ -35,18 +35,6 @@ export const setupBlob = () => { } }; -export const parseXMLFile = (filePath: fs.PathOrFileDescriptor, mimeType: DOMParserSupportedType) => { - const stream = fs.readFileSync(filePath, { encoding: 'utf-8' }); - - let content = stream.toString(); - // Hack for SVG exported from the browser ... - if (mimeType == 'image/svg+xml') { - content = content.replace(' { const parser = new DOMParser(); const xmlDoc = parser.parseFromString(xmlStr, mimeType); @@ -61,6 +49,18 @@ export const parseXMLString = (xmlStr: string, mimeType: DOMParserSupportedType) return xmlDoc; }; +export const parseXMLFile = (filePath: fs.PathOrFileDescriptor, mimeType: DOMParserSupportedType) => { + const stream = fs.readFileSync(filePath, { encoding: 'utf-8' }); + + let content = stream.toString(); + // Hack for SVG exported from the browser ... + if (mimeType === 'image/svg+xml') { + content = content.replace(' { const actualStr = await exporter.export(); diff --git a/packages/mindplot/test/unit/export/expected/complex.md b/packages/mindplot/test/unit/export/expected/complex.md index 349e0792..1889f50c 100644 --- a/packages/mindplot/test/unit/export/expected/complex.md +++ b/packages/mindplot/test/unit/export/expected/complex.md @@ -14,7 +14,6 @@ - Goals - Formulize - Probono - - null diff --git a/packages/mindplot/test/unit/export/expected/complex.txt b/packages/mindplot/test/unit/export/expected/complex.txt index 0e26cca9..e08f33ea 100644 --- a/packages/mindplot/test/unit/export/expected/complex.txt +++ b/packages/mindplot/test/unit/export/expected/complex.txt @@ -13,7 +13,6 @@ 1.11.1 Goals 1.11.2 Formulize 1.12 Probono - 1.12.1 null 2 Strategy 2: Talent Development 2.1 Strategic Priority 2a: Personal Plans 2.2 Strategic Priority 2b: External learning matches organ. goals @@ -23,8 +22,7 @@ 3.1 Strategic Priority 4a:Feedback 3.2 Strategic Priority 4b: Anti Harassment 3.3 Strategic Priority 4c: Diversity - 3.4 null - 3.5 So That... + 3.4 So That... 4 Strategy 1: Recruit & Retain 4.1 So that... 4.2 Strategic Priority 1a: Recruitment @@ -44,22 +42,14 @@ 5.1 Goals 5.1.1 Increase new clients 5.1.1.1 Academic Research - 5.1.1.2 null 5.1.2 Support New Products 5.1.2.1 Formulize - 5.1.2.2 null - 5.1.2.3 null 5.1.3 Support CiviCRM 5.1.4 Identify Opportunites - 5.1.4.1 null - 5.1.4.2 null - 5.1.4.3 null - 5.1.4.4 null 6 Hosting NG Plan 7 Freeform IT Plan 7.1 Fragile 7.2 Tools - 7.3 null 8 Project Teams 8.1 Projects 1-3 8.2 Projects 4-6 @@ -78,7 +68,6 @@ 10.5 Business Plan 11 Strategy 3: Safety and Wellness 11.1 Strategic Priority 3a: H&S Policies & Practices - 11.1.1 null 11.2 Strategic Priority 3b: Health Promotion 11.2.1 Health and Wellness Committee 11.2.2 Work-life Balance Initiative [link: http://hrcouncil.ca/hr-toolkit/workplaces-health-safety.cfm] @@ -101,54 +90,45 @@ 14 Backlog Plan [link: https://docs.google.com/a/freeform.ca/drawings/d/1mrtkVAN3_XefJJCgfxw4Va6xk9TVDBKXDt_uzyIF4Us/edit] 14.1 Go To Backlog Plan [link: https://docs.google.com/a/freeform.ca/drawings/d/1mrtkVAN3_XefJJCgfxw4Va6xk9TVDBKXDt_uzyIF4Us/edit] 15 Strategy Prospecting - 15.1 null - 15.2 null - 15.3 null 16 Stategies: Forecasting - 16.1 null - 16.2 null - 16.3 null 17 Strategies Marketing -18 null -19 Exit Interviews - 19.1 As Freeform - 19.2 Responsiblity: HZ, KS - 19.3 Release - 19.4 Have Heather write procedures for exit interview process - 19.5 So that -20 3 Month Onboarding Process -21 Human Resources Plan - 21.1 Related Org Objectives - 21.1.1 1 - 21.1.2 2 - 21.1.3 3 - 21.1.4 4 - 21.2 Related Documents - 21.3 Goals - 21.3.1 Goal:Staff=Optimal Bus. Growth - 21.3.1.1 So that... - 21.3.1.2 Related Strategic Priorities: - 21.3.1.3 KPI: HR Level equals Planned Growth - 21.3.1.4 Methodology - 21.3.1.4.1 Target - 21.3.2 Goal: Increase Job Satisfaction - 21.3.2.1 So That - 21.3.2.2 Related Strategic Priorities - 21.3.2.2.1 null - 21.3.2.3 KPI: Employee Satisfaction - 21.3.2.3.1 null - 21.3.2.4 Methodology - 21.3.2.4.1 Target - 21.3.3 Goal: Improve Performance - 21.3.3.1 So That - 21.3.3.2 Related Strategic Priorities - 21.3.3.3 KPI: Employee Performance - 21.3.3.4 Methodology - 21.3.3.4.1 Target - 21.3.4 Goal: Reduce Turnover - 21.3.4.1 So That - 21.3.4.2 Related Strategic Priorities - 21.3.4.3 KPI: Retention Rate - 21.3.4.4 Methodology - 21.3.4.4.1 Target - 21.3.5 Risk & Compliance +18 Exit Interviews + 18.1 As Freeform + 18.2 Responsiblity: HZ, KS + 18.3 Release + 18.4 Have Heather write procedures for exit interview process + 18.5 So that +19 3 Month Onboarding Process +20 Human Resources Plan + 20.1 Related Org Objectives + 20.1.1 1 + 20.1.2 2 + 20.1.3 3 + 20.1.4 4 + 20.2 Related Documents + 20.3 Goals + 20.3.1 Goal:Staff=Optimal Bus. Growth + 20.3.1.1 So that... + 20.3.1.2 Related Strategic Priorities: + 20.3.1.3 KPI: HR Level equals Planned Growth + 20.3.1.4 Methodology + 20.3.1.4.1 Target + 20.3.2 Goal: Increase Job Satisfaction + 20.3.2.1 So That + 20.3.2.2 Related Strategic Priorities + 20.3.2.3 KPI: Employee Satisfaction + 20.3.2.4 Methodology + 20.3.2.4.1 Target + 20.3.3 Goal: Improve Performance + 20.3.3.1 So That + 20.3.3.2 Related Strategic Priorities + 20.3.3.3 KPI: Employee Performance + 20.3.3.4 Methodology + 20.3.3.4.1 Target + 20.3.4 Goal: Reduce Turnover + 20.3.4.1 So That + 20.3.4.2 Related Strategic Priorities + 20.3.4.3 KPI: Retention Rate + 20.3.4.4 Methodology + 20.3.4.4.1 Target + 20.3.5 Risk & Compliance