Skip to content

Commit

Permalink
HtmlEditor: ignore errors after move to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeniyKiyashko committed Jun 14, 2024
1 parent 0f9d7e6 commit 935aa33
Show file tree
Hide file tree
Showing 53 changed files with 5,451 additions and 5,004 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import ConverterController from '../converterController';
import ConverterController from '../m_converterController';

class DeltaConverter {
quillInstance: any;

setQuillInstance(quillInstance) {
this.quillInstance = quillInstance;
}

toHtml() {
if(!this.quillInstance) {
return;
}
setQuillInstance(quillInstance) {
this.quillInstance = quillInstance;
}

return this._isQuillEmpty() ?
'' :
this.quillInstance.getSemanticHTML(0, this.quillInstance.getLength() + 1);
toHtml() {
if (!this.quillInstance) {
return;
}

_isQuillEmpty() {
const delta = this.quillInstance.getContents();
return this._isQuillEmpty()
? ''
: this.quillInstance.getSemanticHTML(0, this.quillInstance.getLength() + 1);
}

return delta.length() === 1 && this._isDeltaEmpty(delta);
}
_isQuillEmpty() {
const delta = this.quillInstance.getContents();

_isDeltaEmpty(delta) {
return delta.reduce((__, { insert }) => insert.indexOf('\n') !== -1);
}
return delta.length() === 1 && this._isDeltaEmpty(delta);
}

_isDeltaEmpty(delta) {
return delta.reduce((__, { insert }) => insert.indexOf('\n') !== -1);
}
}

ConverterController.addConverter('delta', DeltaConverter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,63 @@

import TurnDown from 'turndown';
import { getWindow } from '@js/core/utils/window';
import Errors from '@js/ui/widget/ui.errors';
import ShowDown from 'showdown';
import TurnDown from 'turndown';

import { getWindow } from '../../../core/utils/window';
import Errors from '../../widget/ui.errors';
import converterController from '../converterController';
import converterController from '../m_converterController';

class MarkdownConverter {
constructor() {
const window = getWindow();
const turndown = window && window.TurndownService || TurnDown;
const showdown = window && window.showdown || ShowDown;

if(!turndown) {
throw Errors.Error('E1041', 'Turndown');
}

if(!showdown) {
throw Errors.Error('E1041', 'Showdown');
}

this._html2Markdown = new turndown();

if(this._html2Markdown?.addRule) {
this._html2Markdown.addRule('emptyLine', {
filter: (element) => {
return element.nodeName.toLowerCase() === 'p' && element.innerHTML === '<br>';
},
replacement: function() {
return '<br>';
}
});
this._html2Markdown.keep(['table']);
}

this._markdown2Html = new showdown.Converter({
simpleLineBreaks: true,
strikethrough: true,
tables: true
});
_markdown2Html: any;

_html2Markdown: any;

constructor() {
const window = getWindow();
// @ts-expect-error
const turndown = window && window.TurndownService || TurnDown;
// @ts-expect-error
const showdown = window && window.showdown || ShowDown;

if (!turndown) {
throw Errors.Error('E1041', 'Turndown');
}

toMarkdown(htmlMarkup) {
return this._html2Markdown.turndown(htmlMarkup || '');
if (!showdown) {
throw Errors.Error('E1041', 'Showdown');
}

toHtml(markdownMarkup) {
let markup = this._markdown2Html.makeHtml(markdownMarkup);
// eslint-disable-next-line new-cap
this._html2Markdown = new turndown();

if(markup) {
markup = markup.replace(new RegExp('\\r?\\n', 'g'), '');
}
if (this._html2Markdown?.addRule) {
this._html2Markdown.addRule('emptyLine', {
filter: (element) => element.nodeName.toLowerCase() === 'p' && element.innerHTML === '<br>',
replacement() {
return '<br>';
},
});
this._html2Markdown.keep(['table']);
}

this._markdown2Html = new showdown.Converter({
simpleLineBreaks: true,
strikethrough: true,
tables: true,
});
}

toMarkdown(htmlMarkup) {
return this._html2Markdown.turndown(htmlMarkup || '');
}

return markup;
toHtml(markdownMarkup) {
let markup = this._markdown2Html.makeHtml(markdownMarkup);

if (markup) {
markup = markup.replace(new RegExp('\\r?\\n', 'g'), '');
}

return markup;
}
}

converterController.addConverter('markdown', MarkdownConverter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Quill from 'devextreme-quill';

// eslint-disable-next-line import/no-mutable-exports
let AlignStyle = {};

if(Quill) {
AlignStyle = Quill.import('attributors/style/align');
AlignStyle.whitelist.push('left');
if (Quill) {
AlignStyle = Quill.import('attributors/style/align');
// @ts-expect-error
AlignStyle.whitelist.push('left');
}

export default AlignStyle;
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Quill from 'devextreme-quill';

// eslint-disable-next-line import/no-mutable-exports
let FontStyle = {};

if(Quill) {
FontStyle = Quill.import('attributors/style/font');
FontStyle.whitelist = null;
if (Quill) {
FontStyle = Quill.import('attributors/style/font');
// @ts-expect-error
FontStyle.whitelist = null;
}

export default FontStyle;
123 changes: 63 additions & 60 deletions packages/devextreme/js/__internal/ui/html_editor/formats/m_image.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,70 @@
import { isObject } from '@js/core/utils/type';
import Quill from 'devextreme-quill';
import { isObject } from '../../../core/utils/type';

// eslint-disable-next-line import/no-mutable-exports
let ExtImage = {};

if(Quill) {
const Image = Quill.import('formats/image');

ExtImage = class ExtImage extends Image {
static create(data) {
const SRC = data && data.src || data;
const node = super.create(SRC);

if(isObject(data)) {
const setAttribute = (attr, value) => {
data[attr] && node.setAttribute(attr, value);
};

setAttribute('alt', data.alt);
setAttribute('width', data.width);
setAttribute('height', data.height);
}

return node;
}

static formats(domNode) {
const formats = super.formats(domNode);

formats['imageSrc'] = domNode.getAttribute('src');

return formats;
}

formats() {
const formats = super.formats();
const floatValue = this.domNode.style['float'];

if(floatValue) {
formats['float'] = floatValue;
}

return formats;
}

format(name, value) {
if(name === 'float') {
this.domNode.style[name] = value;
} else {
super.format(name, value);
}
}

static value(domNode) {
return {
src: domNode.getAttribute('src'),
width: domNode.getAttribute('width'),
height: domNode.getAttribute('height'),
alt: domNode.getAttribute('alt')
};
}
};

ExtImage.blotName = 'extendedImage';
if (Quill) {
const Image = Quill.import('formats/image');

ExtImage = class ExtImage extends Image {
static create(data) {
const SRC = data && data.src || data;
const node = super.create(SRC);

if (isObject(data)) {
const setAttribute = (attr, value) => {
data[attr] && node.setAttribute(attr, value);
};
// @ts-expect-error
setAttribute('alt', data.alt);
// @ts-expect-error
setAttribute('width', data.width);
// @ts-expect-error
setAttribute('height', data.height);
}

return node;
}

static formats(domNode) {
const formats = super.formats(domNode);

formats.imageSrc = domNode.getAttribute('src');

return formats;
}

formats() {
const formats = super.formats();
const floatValue = this.domNode.style.float;

if (floatValue) {
formats.float = floatValue;
}

return formats;
}

format(name, value) {
if (name === 'float') {
this.domNode.style[name] = value;
} else {
super.format(name, value);
}
}

static value(domNode) {
return {
src: domNode.getAttribute('src'),
width: domNode.getAttribute('width'),
height: domNode.getAttribute('height'),
alt: domNode.getAttribute('alt'),
};
}
};
// @ts-expect-error
ExtImage.blotName = 'extendedImage';
}

export default ExtImage;
Loading

0 comments on commit 935aa33

Please sign in to comment.