From 4b47b7d7ba308a6d93070f2aee961c85bdff0841 Mon Sep 17 00:00:00 2001 From: Nicolas Bayet Date: Tue, 2 Jun 2020 10:11:35 +0200 Subject: [PATCH] temp --- packages/plugin-link/src/Link.ts | 10 +++ packages/plugin-link/src/LinkFormat.ts | 2 +- packages/plugin-link/test/LinkFormat.test.ts | 17 +++++ .../plugin-odoo-snippets/src/OdooSnippet.ts | 69 +++++++++++++++++-- 4 files changed, 92 insertions(+), 6 deletions(-) diff --git a/packages/plugin-link/src/Link.ts b/packages/plugin-link/src/Link.ts index 368688a59..f31c1ce32 100644 --- a/packages/plugin-link/src/Link.ts +++ b/packages/plugin-link/src/Link.ts @@ -14,10 +14,16 @@ import { Keymap } from '../../plugin-keymap/src/Keymap'; import { Layout } from '../../plugin-layout/src/Layout'; import linkForm from '../assets/LinkForm.xml'; import { Owl } from '../../plugin-owl/src/Owl'; +import { Attributes } from '../../plugin-xml/src/Attributes'; export interface LinkParams extends CommandParams { label?: string; url?: string; + /** + * The target of an html anchor. + * Could be "_blank", "_self" ,"_parent", "_top" or the framename. + */ + target?: string; } export class Link extends JWPlugin { @@ -72,11 +78,15 @@ export class Link extends JWPlugin // } const link = new LinkFormat(params.url); + if (params.target) { + link.modifiers.get(Attributes).set('target', params.target); + } // // TODO: modal re-using url // if (!link) return; // if (range.isCollapsed()) { console.log('params:', params); + console.log('link:', link); this.editor.execCommand('insertText', { text: params.label || link.url, formats: new Modifiers(link), diff --git a/packages/plugin-link/src/LinkFormat.ts b/packages/plugin-link/src/LinkFormat.ts index dcdec0861..65f659508 100644 --- a/packages/plugin-link/src/LinkFormat.ts +++ b/packages/plugin-link/src/LinkFormat.ts @@ -1,7 +1,7 @@ import { Format } from '../../plugin-inline/src/Format'; export class LinkFormat extends Format { - constructor(public url = '#') { + constructor(public url = '#', public target = '') { super('A'); } diff --git a/packages/plugin-link/test/LinkFormat.test.ts b/packages/plugin-link/test/LinkFormat.test.ts index 4eac63974..82dd0171c 100644 --- a/packages/plugin-link/test/LinkFormat.test.ts +++ b/packages/plugin-link/test/LinkFormat.test.ts @@ -1,5 +1,8 @@ import { LinkFormat } from '../src/LinkFormat'; import { expect } from 'chai'; +import { testEditor } from '../../utils/src/testUtils'; +import { BasicEditor } from '../../../bundles/BasicEditor'; +import { Layout } from '../../plugin-layout/src/Layout'; describe('Link', () => { describe('LinkFormat', () => { describe('clone()', () => { @@ -9,4 +12,18 @@ describe('Link', () => { }); }); }); + describe('url & attributes', () => { + it.only('url & attributes', async () => { + await testEditor(BasicEditor, { + contentBefore: 'test', + stepFunction: editor => { + const domEngine = editor.plugins.get(Layout).engines.dom; + const editable = domEngine.components.get('editable')[0]; + console.log('editable first:', editable.firstChild); + debugger; + }, + contentAfter: 'foo', + }); + }); + }); }); diff --git a/packages/plugin-odoo-snippets/src/OdooSnippet.ts b/packages/plugin-odoo-snippets/src/OdooSnippet.ts index 5b10f5ef9..a766e9773 100644 --- a/packages/plugin-odoo-snippets/src/OdooSnippet.ts +++ b/packages/plugin-odoo-snippets/src/OdooSnippet.ts @@ -17,6 +17,10 @@ import { CommandParams } from '../../core/src/Dispatcher'; import { InlineNode } from '../../plugin-inline/src/InlineNode'; import { HtmlDomRenderingEngine } from '../../plugin-html/src/HtmlDomRenderingEngine'; import { LinkFormat } from '../../plugin-link/src/LinkFormat'; +import { Format } from '../../plugin-inline/src/Format'; +import { Inline } from '../../plugin-inline/src/Inline'; +import { Link } from '../../plugin-link/src/Link'; +import { CharNode } from '../../plugin-char/src/CharNode'; export interface RemoveClassParams extends CommandParams { elements: VElement[]; @@ -26,6 +30,12 @@ export interface AddClassParams extends CommandParams { elements?: VElement[]; classes: string[]; } +export interface AddClassToLinkParams extends CommandParams { + /** + * The class attribute to attatch to the link. + */ + classes: string; +} export interface ToggleClassParams { nodes: VNode[]; class: string; @@ -70,6 +80,25 @@ export interface ReplaceParams { nodes: VNode[]; html: string; } +interface SelectedLinkInfo { + /** + * The selected text + */ + text: string; + /** + * The url of the link + */ + url: string; + /** + * The css class associated with the link + */ + class: string; + /** + * The target of an html anchor. + * Could be "_blank", "_self" ,"_parent", "_top" or the framename. + */ + target: string; +} export class OdooSnippet extends JWPlugin { readonly loadables: Loadables = { @@ -84,6 +113,9 @@ export class OdooSnippet extends JWPl addClasses: { handler: this.addClasses.bind(this), }, + addClassToLink: { + handler: this.addClassToLink.bind(this), + }, toggleClass: { handler: this.toggleClass.bind(this), }, @@ -114,11 +146,8 @@ export class OdooSnippet extends JWPl getRecordCover: { handler: this.getRecordCover.bind(this), }, - getSelectedText: { - handler: this.getSelectedText.bind(this), - }, - getSelectedLink: { - handler: this.getSelectedLink.bind(this), + getLinkInfo: { + handler: this.getLinkInfo.bind(this), }, }; @@ -149,6 +178,20 @@ export class OdooSnippet extends JWPl } } } + addClassToLink(params: AddClassToLinkParams): void { + console.log('addclasstolink'); + console.log('params.context.range:', params.context.range); + console.log('params.context.range.start:', params.context.range.start); + console.log('params.context.range.targetedNodes():', params.context.range.targetedNodes()); + console.log( + 'params.context.range.traversedNodes():', + params.context.range.traversedNodes(), + ); + const nodes = params.context.range.targetedNodes(InlineNode); + for (const node of nodes) { + node.modifiers.get(Attributes).set('class', params.classes); + } + } toggleClass(params: ToggleClassParams): void { for (const node of params.nodes) { const classList = node.modifiers.get(Attributes).classList; @@ -264,6 +307,22 @@ export class OdooSnippet extends JWPl ) as LinkFormat); return linkFormat ? linkFormat.url : ''; } + getLinkInfo(params: CommandParams): SelectedLinkInfo { + const targettedNodes = params.context.range.targetedNodes(CharNode); + const text = targettedNodes.map(x => x.char).join(''); + // + const inline = this.editor.plugins.get(Inline); + const modifiers = inline.getCurrentModifiers(params.context.range); + return { + text: text, + url: modifiers.get(LinkFormat)?.url, + class: modifiers.get(Attributes)?.get('class'), + target: modifiers + .get(LinkFormat) + ?.modifiers?.get(Attributes) + ?.get('target'), + } as SelectedLinkInfo; + } async _parseHTMLString(content: string): Promise { const parser = this.editor.plugins.get(Parser);