/**
* @module plugins/paste
*/
import type {
IDialog,
IJodit,
InsertMode,
IUIOption,
Nullable
} from 'jodit/types';
import {
isArray,
isNumber,
isString,
isVoid
} from 'jodit/core/helpers/checker';
import { Dom } from 'jodit/core/dom/dom';
import { TEXT_PLAIN } from 'jodit/core/constants';
import { Button } from 'jodit/core/ui/button/button/button';
import type { PasteEvent } from './interface';
/**
* Remove special HTML comments
*/
function removeExtraFragments(html: string): string {
html = html.replace(/]+?>/g, '');
const start = html.search(//i);
if (start !== -1) {
html = html.substring(start + 20);
}
const end = html.search(//i);
if (end !== -1) {
html = html.substring(0, end);
}
return html;
}
function isDragEvent(e: Nullable): e is DragEvent {
return Boolean(e && e.type === 'drop');
}
/**
* One insert point for clipboard plugins
*/
export function pasteInsertHtml(
e: Nullable,
editor: IJodit,
html: number | string | Node
): void {
if (editor.isInDestruct) {
return;
}
if (isDragEvent(e)) {
editor.s.insertCursorAtPoint(e.clientX, e.clientY);
}
const result = editor.e.fire('beforePasteInsert', html);
if (
!isVoid(result) &&
(isString(result) || isNumber(result) || Dom.isNode(result))
) {
html = result;
}
if (isString(html)) {
html = removeExtraFragments(html);
}
editor.s.insertHTML(html);
}
/**
* Return all available data types in event
*/
export function getAllTypes(dt: DataTransfer): string {
const types: ReadonlyArray | string = dt.types;
let types_str: string = '';
if (
isArray(types) ||
{}.toString.call(types) === '[object DOMStringList]'
) {
for (let i = 0; i < types.length; i += 1) {
types_str += types[i] + ';';
}
} else {
types_str = (types || TEXT_PLAIN).toString() + ';';
}
return types_str;
}
/**
* Make command dialog
*/
export function askInsertTypeDialog(
jodit: IJodit,
msg: string,
title: string,
callback: (yes: InsertMode) => void,
buttonList: IUIOption[]
): IDialog | void {
if (
jodit.e.fire(
'beforeOpenPasteDialog',
msg,
title,
callback,
buttonList
) === false
) {
return;
}
const dialog = jodit.confirm(
`${jodit.i18n(
msg
)}
`,
jodit.i18n(title)
);
const buttons = buttonList.map(({ text, value }) =>
Button(jodit, {
text,
name: text.toLowerCase(),
tabIndex: 0
}).onAction(() => {
dialog.close();
callback(value as InsertMode);
})
);
dialog.e.one(dialog, 'afterClose', () => {
if (!jodit.s.isFocused()) {
jodit.s.focus();
}
});
const cancel = Button(jodit, {
text: 'Cancel',
tabIndex: 0
}).onAction(() => {
dialog.close();
});
dialog.setFooter([...buttons, cancel]);
buttons[0].focus();
buttons[0].state.variant = 'primary';
jodit.e.fire(
'afterOpenPasteDialog',
dialog,
msg,
title,
callback,
buttonList
);
return dialog;
}