Browse Source

Move ComponentLink to TS

ts-components
Artur Arseniev 3 years ago
parent
commit
4b5b6bd0c7
  1. 43
      src/dom_components/model/ComponentLink.js
  2. 44
      src/dom_components/model/ComponentLink.ts

43
src/dom_components/model/ComponentLink.js

@ -1,43 +0,0 @@
import { forEach } from 'underscore';
import { toLowerCase } from 'utils/mixins';
import ComponentText from './ComponentText';
const type = 'link';
export default class ComponentLink extends ComponentText {
get defaults() {
return {
...super.defaults,
type,
tagName: 'a',
traits: ['title', 'href', 'target'],
};
}
}
ComponentLink.isComponent = (el, opts = {}) => {
let result;
if (toLowerCase(el.tagName) === 'a') {
const textTags = opts.textTags || [];
result = { type, editable: false };
// The link is editable only if, at least, one of its
// children is a text node (not empty one)
const children = el.childNodes;
const len = children.length;
if (!len) delete result.editable;
forEach(children, child => {
const { tagName } = child;
if (
(child.nodeType == 3 && child.textContent.trim() !== '') ||
(tagName && textTags.indexOf(toLowerCase(tagName)) >= 0)
) {
delete result.editable;
}
});
}
return result;
};

44
src/dom_components/model/ComponentLink.ts

@ -0,0 +1,44 @@
import { forEach } from 'underscore';
import { toLowerCase } from '../../utils/mixins';
import ComponentText from './ComponentText';
const type = 'link';
export default class ComponentLink extends ComponentText {
get defaults() {
return {
// @ts-ignore
...super.defaults,
type,
tagName: 'a',
traits: ['title', 'href', 'target'],
};
}
static isComponent(el: HTMLElement, opts: any = {}) {
let result: any;
if (toLowerCase(el.tagName) === 'a') {
const textTags = opts.textTags || [];
result = { type, editable: false };
// The link is editable only if, at least, one of its
// children is a text node (not empty one)
const children = el.childNodes;
const len = children.length;
if (!len) delete result.editable;
forEach(children, child => {
const { tagName } = child as HTMLElement;
if (
(child.nodeType == 3 && (child as any).textContent.trim() !== '') ||
(tagName && textTags.indexOf(toLowerCase(tagName)) >= 0)
) {
delete result.editable;
}
});
}
return result;
}
}
Loading…
Cancel
Save