Improve paste behaviour (#473)

* Improve paste behaviour

* Use variables for long expressions

* Fix paste processing logic
This commit is contained in:
George Berezhnoy 2018-10-16 15:33:49 +03:00 committed by GitHub
parent 4e2b662bee
commit cb86bf446b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 16 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,6 @@
{
"name": "codex.editor",
"version": "2.1.2",
"version": "2.1.3",
"description": "Codex Editor. Native JS, based on API and Open Source",
"main": "build/codex-editor.js",
"scripts": {

View file

@ -91,6 +91,11 @@ export default class Paste extends Module {
*/
private toolsTags: {[tag: string]: ITagSubstitute} = {};
/**
* Store tags to substitute by tool name
*/
private tagsByTool: {[tools: string]: string[]} = {};
/** Patterns` substitutions parameters */
private toolsPatterns: IPatternSubstitute[] = [];
@ -242,6 +247,8 @@ export default class Paste extends Module {
tool: name,
};
});
this.tagsByTool[name] = tags.map((t) => t.toUpperCase());
}
/**
@ -687,23 +694,32 @@ export default class Paste extends Module {
case Node.ELEMENT_NODE:
const element = node as HTMLElement;
const {tool = ''} = this.toolsTags[element.tagName] || {};
const toolTags = this.tagsByTool[tool] || [];
const isSubstitutable = tags.includes(element.tagName);
const isBlockElement = $.blockElements.includes(element.tagName.toLowerCase());
const containsAnotherToolTags = Array
.from(element.children)
.some(
({tagName}) => tags.includes(tagName) && !toolTags.includes(tagName),
);
const containsBlockElements = Array.from(element.children).some(
({tagName}) => $.blockElements.includes(tagName.toLowerCase()),
);
/** Append inline elements to previous fragment */
if (
!$.blockElements.includes(element.tagName.toLowerCase()) &&
!tags.includes(element.tagName)
) {
if (!isBlockElement && !isSubstitutable) {
destNode.appendChild(element);
return [...nodes, destNode];
}
if (tags.includes(element.tagName) || (
$.blockElements.includes(element.tagName.toLowerCase()) &&
Array.from(element.children).every(
({tagName}) => !$.blockElements.includes(tagName.toLowerCase()),
)
)
if (
(isSubstitutable && !containsAnotherToolTags) ||
(isBlockElement && !containsBlockElements && !containsAnotherToolTags )
) {
return [...nodes, element];
return [...nodes, destNode, element];
}
break;