Skip to content

Commit

Permalink
Refactoring in clipboard plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
xdan committed Aug 6, 2020
1 parent 812b1fe commit 52d80b0
Show file tree
Hide file tree
Showing 13 changed files with 758 additions and 658 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"lint-staged": "^10.2.11",
"merge-stream": "^2.0.0",
"mini-css-extract-plugin": "^0.9.0",
"mocha": "^8.1.0",
"mocha": "^8.1.1",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"postcss-css-variables": "^0.17.0",
"postcss-loader": "^3.0.0",
Expand Down
3 changes: 1 addition & 2 deletions src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export const IS_IE =
/**
* @property {string} TEXT_PLAIN='text/plain' For IE11 it will be 'text'. Need for dataTransfer.setData
*/
export const URL_LIST = IS_IE ? 'url' : 'text/uri-list';
export const TEXT_PLAIN = IS_IE ? 'text' : 'text/plain';
export const TEXT_HTML = IS_IE ? 'text' : 'text/html';

Expand All @@ -106,7 +105,7 @@ export const MARKER_CLASS = 'jodit-selection_marker';
export const EMULATE_DBLCLICK_TIMEOUT = 300;

export const INSERT_AS_HTML = 'insert_as_html';
export const INSERT_CLEAR_HTML = 'insert_clear_html';
export const INSERT_CLEAR_HTML = 'insert_clear_html';
export const INSERT_AS_TEXT = 'insert_as_text';
export const INSERT_ONLY_TEXT = 'insert_only_text';

Expand Down
143 changes: 143 additions & 0 deletions src/plugins/clipboard/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*!
* Jodit Editor (https://xdsoft.net/jodit/)
* Released under MIT see LICENSE.txt in the project root for license information.
* Copyright (c) 2013-2020 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/
import { Config } from '../../config';
import {
INSERT_AS_HTML,
INSERT_CLEAR_HTML,
INSERT_ONLY_TEXT,
INSERT_AS_TEXT
} from '../../core/constants';
import { IControlType, IJodit } from '../../types';
import { pluginKey as clipboardPluginKey } from './cut';
import { Alert } from '../../modules/dialog';
import { pasteInsertHtml } from './paste/helpers';

export type PasteEvent = ClipboardEvent | DragEvent;
export type InsertMode =
| typeof INSERT_AS_HTML
| typeof INSERT_CLEAR_HTML
| typeof INSERT_ONLY_TEXT
| typeof INSERT_AS_TEXT;

declare module '../../config' {
interface Config {
/**
* Ask before paste HTML in WYSIWYG mode
*/
askBeforePasteHTML: boolean;
processPasteHTML: boolean;

askBeforePasteFromWord: boolean;
processPasteFromWord: boolean;

/**
* Inserts HTML line breaks before all newlines in a string
*/
nl2brInPlainText: boolean;

/**
* Default insert method
*/
defaultActionOnPaste: InsertMode;

/**
* Draggable elements
*/
draggableTags: string | string[];
}
}

Config.prototype.askBeforePasteHTML = true;
Config.prototype.processPasteHTML = true;

Config.prototype.askBeforePasteFromWord = true;
Config.prototype.processPasteFromWord = true;

Config.prototype.nl2brInPlainText = true;
Config.prototype.defaultActionOnPaste = INSERT_AS_HTML;

Config.prototype.draggableTags = ['img', 'a', 'jodit-media', 'jodit'];

Config.prototype.controls.cut = {
command: 'cut',
isDisabled: (editor: IJodit) => editor.s.isCollapsed(),
tooltip: 'Cut selection'
} as IControlType;

Config.prototype.controls.copy = {
command: 'copy',
isDisabled: (editor: IJodit) => editor.s.isCollapsed(),
tooltip: 'Copy selection'
} as IControlType;

const psKey = 'pasteStorage';
Config.prototype.controls.paste = {
tooltip: 'Paste from clipboard',

async exec(editor: IJodit, _, { control }) {
if (control.name === psKey) {
editor.execCommand('showPasteStorage');
return;
}

editor.s.focus();

let text = '',
error = true;

if (error) {
text = editor.buffer.get<string>(clipboardPluginKey) || '';
error = text.length === 0;
}

if (error && navigator.clipboard) {
try {
const items = await (navigator.clipboard as any).read();

if (items && items.length) {
const textBlob = await items[0].getType('text/plain');
text = await new Response(textBlob).text();
}
} catch {}

if (error) {
try {
text = await navigator.clipboard.readText();
error = false;
} catch {}
}
}

if (error) {
const value = editor.value;
editor.ed.execCommand('paste');
error = value !== editor.value;
}

if (text) {
pasteInsertHtml(editor, text);
} else {
if (error) {
Alert(
editor.i18n(
"Your browser doesn't support direct access to the clipboard."
),
() => {
editor.s.focus();
}
).bindDestruct(editor);
}
}
},

list: {
[psKey]: 'Paste Storage'
},

isChildDisabled(j): boolean {
return j.e.fire('pasteStorageList') < 2;
}
} as IControlType;
17 changes: 2 additions & 15 deletions src/plugins/clipboard/cut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,10 @@
* Copyright (c) 2013-2020 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/

import { IControlType, IJodit, IPlugin } from '../../types';
import { Config } from '../../config';
import { IJodit, IPlugin } from '../../types';
import { TEXT_HTML, TEXT_PLAIN } from '../../core/constants';
import { stripTags } from '../../core/helpers';
import { getDataTransfer } from './paste';

Config.prototype.controls.cut = {
command: 'cut',
isDisabled: (editor: IJodit) => editor.s.isCollapsed(),
tooltip: 'Cut selection'
} as IControlType;

Config.prototype.controls.copy = {
command: 'copy',
isDisabled: (editor: IJodit) => editor.s.isCollapsed(),
tooltip: 'Copy selection'
} as IControlType;
import { getDataTransfer } from './paste/helpers';

export const pluginKey = 'clipboard';

Expand Down
12 changes: 0 additions & 12 deletions src/plugins/clipboard/drag-and-drop-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,11 @@
* Copyright (c) 2013-2020 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/

import { Config } from '../../config';
import { css, ctrlKey, dataBind, splitArray } from '../../core/helpers';
import { Plugin } from '../../core/plugin';
import { Dom } from '../../core/dom';
import { getContainer } from '../../core/global';

declare module '../../config' {
interface Config {
draggableTags: string | string[];
}
}

/**
* Draggable elements
*/
Config.prototype.draggableTags = ['img', 'a', 'jodit-media', 'jodit'];

/**
* Process drag and drop image or another element inside the editor
*/
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/clipboard/drag-and-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Dom } from '../../core/dom';
import { attr, css, ctrlKey, dataBind } from '../../core/helpers';
import { Plugin } from '../../core/plugin';
import { IPoint } from '../../types';
import { getDataTransfer } from './index';
import { getDataTransfer } from './paste/helpers';
import { getContainer } from '../../core/global';

/**
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/clipboard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* Copyright (c) 2013-2020 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/

import './config';
export * from './cut';
export * from './paste';
export * from './paste/paste';
export * from './paste-storage/paste-storage';
import './copy-format';
4 changes: 4 additions & 0 deletions src/plugins/clipboard/paste-storage/paste-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export class pasteStorage extends Plugin {
afterInit(): void {
this.j.e
.off('afterCopy.paste-storage')
.on('pasteStorageList.paste-storage', () => this.list.length)
.on('afterCopy.paste-storage', (html: string) => {
if (this.list.indexOf(html) !== -1) {
this.list.splice(this.list.indexOf(html), 1);
Expand All @@ -207,6 +208,9 @@ export class pasteStorage extends Plugin {
beforeDestruct(): void {
this.dialog && this.dialog.destruct();

this.j.e
.off('.paste-storage');

Dom.safeRemove(this.previewBox);
Dom.safeRemove(this.listBox);
Dom.safeRemove(this.container);
Expand Down
Loading

0 comments on commit 52d80b0

Please sign in to comment.