Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
Goaman committed Jun 4, 2020
1 parent f37a265 commit 4b47b7d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
10 changes: 10 additions & 0 deletions packages/plugin-link/src/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin<T> {
Expand Down Expand Up @@ -72,11 +78,15 @@ export class Link<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin<T>
// }

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<Char>('insertText', {
text: params.label || link.url,
formats: new Modifiers(link),
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-link/src/LinkFormat.ts
Original file line number Diff line number Diff line change
@@ -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');
}

Expand Down
17 changes: 17 additions & 0 deletions packages/plugin-link/test/LinkFormat.test.ts
Original file line number Diff line number Diff line change
@@ -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()', () => {
Expand All @@ -9,4 +12,18 @@ describe('Link', () => {
});
});
});
describe('url & attributes', () => {
it.only('url & attributes', async () => {
await testEditor(BasicEditor, {
contentBefore: '<a href="foo" class="foo">test</a>',
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',
});
});
});
});
69 changes: 64 additions & 5 deletions packages/plugin-odoo-snippets/src/OdooSnippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand All @@ -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;
Expand Down Expand Up @@ -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<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin<T> {
readonly loadables: Loadables<Parser & Renderer & Keymap> = {
Expand All @@ -84,6 +113,9 @@ export class OdooSnippet<T extends JWPluginConfig = JWPluginConfig> extends JWPl
addClasses: {
handler: this.addClasses.bind(this),
},
addClassToLink: {
handler: this.addClassToLink.bind(this),
},
toggleClass: {
handler: this.toggleClass.bind(this),
},
Expand Down Expand Up @@ -114,11 +146,8 @@ export class OdooSnippet<T extends JWPluginConfig = JWPluginConfig> 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),
},
};

Expand Down Expand Up @@ -149,6 +178,20 @@ export class OdooSnippet<T extends JWPluginConfig = JWPluginConfig> 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;
Expand Down Expand Up @@ -264,6 +307,22 @@ export class OdooSnippet<T extends JWPluginConfig = JWPluginConfig> 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<VNode[]> {
const parser = this.editor.plugins.get(Parser);
Expand Down

0 comments on commit 4b47b7d

Please sign in to comment.