Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vinhtranchau committed Apr 6, 2018
1 parent 5ae2191 commit 6673ea0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
7 changes: 6 additions & 1 deletion src/modules/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const each = (obj: Array<any>|any, callback: eachCallback|Function) => {
return obj;
};

each(['Boolean', 'Number', 'String', 'Function', 'Array', 'Date', 'RegExp', 'Object', 'Error', 'Symbol', 'HTMLDocument', 'Window', 'HTMLElement', 'HTMLBodyElement', 'Text', 'DocumentFragment'],
each(['Boolean', 'Number', 'String', 'Function', 'Array', 'Date', 'RegExp', 'Object', 'Error', 'Symbol', 'HTMLDocument', 'Window', 'HTMLElement', 'HTMLBodyElement', 'Text', 'DocumentFragment', 'DOMStringList'],
(i, name) => {
class2type["[object " + name + "]"] = name.toLowerCase();
}
Expand Down Expand Up @@ -792,6 +792,11 @@ export const css = (element: HTMLElement, key: string|{[key: string]: number | s
return normilizeCSSValue(<string>key, result);
};

/**
* Always return Array
* @param a
* @return {Array<any>}
*/
export const asArray = (a: any): Array<any> => (
Array.isArray(a) ? a : [a]
);
Expand Down
15 changes: 1 addition & 14 deletions src/modules/Selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,19 +371,6 @@ export class Select extends Component{
throw new Error('Parameter node most be instance of Node');
}

const isOrContainsNode = (ancestor: Node, descendant: Node): boolean => {
let node: Node|null = descendant;

while (node) {
if (node === ancestor) {
return true;
}
node = node.parentNode;
}

return false;
};

this.focus();

if (this.jodit.editorWindow.getSelection) {
Expand All @@ -395,7 +382,7 @@ export class Select extends Component{

if (sel.rangeCount) {
const range: Range = sel.getRangeAt(0);
if (isOrContainsNode(this.jodit.editor, range.commonAncestorContainer)) {
if (Dom.isOrContains(this.jodit.editor, range.commonAncestorContainer)) {
range.deleteContents();
range.insertNode(node);
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/modules/Uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ export class Uploader {
}

if (browser('ff') || isIE()) {
if (!e.clipboardData.types.length && e.clipboardData.types[0] !== TEXT_PLAIN) {
if (e.clipboardData && (!e.clipboardData.types.length && e.clipboardData.types[0] !== TEXT_PLAIN)) {
const div: HTMLDivElement = <HTMLDivElement>dom('<div tabindex="-1" style="left: -9999px; top: 0; width: 0; height: 100%; line-height: 140%; overflow: hidden; position: fixed; z-index: 2147483647; word-break: break-all;" contenteditable="true"></div>', this.jodit.ownerDocument);
this.jodit.ownerDocument.body.appendChild(div);

Expand Down Expand Up @@ -559,9 +559,8 @@ export class Uploader {
if (hasFiles(event)) {
form.classList.contains('jodit_draghover') ||
form.classList.add('jodit_draghover');
event.preventDefault();
}

event.preventDefault();
})
.on(form, "dragend", (event: DragEvent) => {
if (hasFiles(event)) {
Expand Down
21 changes: 19 additions & 2 deletions src/plugins/DragAndDrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,24 @@ export class DragAndDrop extends Plugin {
}
}, 10);

private bufferRange: Range | null = null;

private onDragStart = (event: DragEvent) => {
let target: HTMLElement = (<HTMLElement>event.target);
this.onDragEnd(); // remove olddraggable

this.isFragmentFromEditor = Dom.isOrContains(this.jodit.editor, target, true);
this.isCopyMode = this.isFragmentFromEditor ? ctrlKey(event) : true; // we can move only element from editor

if (this.isFragmentFromEditor) {
const sel: Selection = this.jodit.editorWindow.getSelection();
const range: Range | null = sel.rangeCount ? sel.getRangeAt(0) : null;
if (range) {
this.bufferRange = range.cloneRange();
}
} else {
this.bufferRange = null;
}

this.startDragPoint.x = event.clientX;
this.startDragPoint.y = event.clientY;
Expand Down Expand Up @@ -97,7 +108,7 @@ export class DragAndDrop extends Plugin {
}

const sel: Selection = this.jodit.editorWindow.getSelection();
const range: Range | null = sel.rangeCount ? sel.getRangeAt(0) : null;
const range: Range | null = this.bufferRange || (sel.rangeCount ? sel.getRangeAt(0) : null);


let fragment: DocumentFragment | HTMLElement | null = null;
Expand All @@ -124,7 +135,13 @@ export class DragAndDrop extends Plugin {
this.jodit.selection.insertCursorAtPoint(event.clientX, event.clientY);

if (fragment) {
this.jodit.selection.insertNode(fragment, false);
this.jodit.selection.insertNode(fragment, false, false);
if (range && fragment.firstChild && fragment.lastChild) {
range.setStartBefore(fragment.firstChild);
range.setEndAfter(fragment.lastChild);
this.jodit.selection.selectRange(range);
this.jodit.events.fire('synchro');
}
}

event.preventDefault();
Expand Down
16 changes: 9 additions & 7 deletions src/plugins/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {Jodit} from '../Jodit';
import {Confirm, Dialog} from '../modules/Dialog';
import {
isHTML, browser, htmlspecialchars, isHTMLFromWord, applyStyles, dom,
cleanFromWord
cleanFromWord, isIE, type, trim
} from '../modules/Helpers';
import {Config} from '../Config';
import {INSERT_AS_HTML, INSERT_AS_TEXT, INSERT_CLEAR_HTML, INSERT_ONLY_TEXT, TEXT_HTML, TEXT_PLAIN} from "../constants";
Expand Down Expand Up @@ -234,23 +234,25 @@ export function paste(editor: Jodit) {
types_str: string = '',
clipboard_html: any = '';

if (Array.isArray(types)) {
if (Array.isArray(types) || type(types) === 'domstringlist') {
for (i = 0; i < types.length; i += 1) {
types_str += types[i] + ";";
}
} else {
types_str = types;
}

if (/text\/html/.test(types_str)) {
if (/text\/html/i.test(types_str)) {
clipboard_html = getDataTransfer(event).getData("text/html");
} else if (/text\/rtf/.test(types_str) && browser('safari')) {
} else if (/text\/rtf/i.test(types_str) && browser('safari')) {
clipboard_html = getDataTransfer(event).getData("text/rtf");
} else if (/text\/plain/.test(types_str) && !browser('mozilla')) {
} else if (/text\/plain/i.test(types_str) && !browser('mozilla')) {
clipboard_html = getDataTransfer(event).getData(TEXT_PLAIN);
} else if (/text/i.test(types_str) && isIE) {
clipboard_html = getDataTransfer(event).getData(TEXT_PLAIN);
}

if (clipboard_html !== '' || clipboard_html instanceof (<any>editor.editorWindow).Node) {
if (clipboard_html instanceof (<any>editor.editorWindow).Node || trim(clipboard_html) !== '') {
/**
* Triggered after the content is pasted from the clipboard into the Jodit. If a string is returned the new string will be used as the pasted content.
*
Expand Down Expand Up @@ -351,7 +353,7 @@ export function paste(editor: Jodit) {
if (getDataTransfer(event).types && [].slice.call(getDataTransfer(event).types).indexOf("text/html") !== -1) {
const html: string = getDataTransfer(event).getData(TEXT_HTML);
return processHTMLData(html);
} else {
} else if (event.type !== 'drop') {
const div: HTMLDivElement = <HTMLDivElement>dom('<div tabindex="-1" style="left: -9999px; top: 0; width: 0; height: 100%; line-height: 140%; overflow: hidden; position: fixed; z-index: 2147483647; word-break: break-all;" contenteditable="true"></div>', editor.ownerDocument);
editor.container.appendChild(div);
const selData = editor.selection.save();
Expand Down

0 comments on commit 6673ea0

Please sign in to comment.