Skip to content

Commit

Permalink
Add option to disable paste handling (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
gohabereg authored Jul 1, 2019
1 parent 38d1abf commit a923846
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
8 changes: 4 additions & 4 deletions dist/editor.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

### 2.15

- `New` — New [`blocks.insert()`](api.md) API method [#715](https://github.com/codex-team/editor.js/issues/715).
- `Deprecated`[`blocks.insertNewBlock()`](api.md) method is deprecated. Use `blocks.insert()` instead.
- `Improvements` — Inline Toolbar now works on mobile devices [#706](https://github.com/codex-team/editor.js/issues/706)
- `Improvements` — Toolbar looks better on mobile devices [#706](https://github.com/codex-team/editor.js/issues/706)
- `Improvements` — Now `pasteConfig` can return `false` to disable paste handling on your Tool [#801](https://github.com/codex-team/editor.js/issues/801)
- `Fix` — EditorConfig's `onChange` callback now fires when native inputs\` content has been changed [#794](https://github.com/codex-team/editor.js/issues/794)
- `New` — New [`blocks.insert()`](api.md) API method [#715](https://github.com/codex-team/editor.js/issues/715).
- `Deprecated`[`blocks.insertNewBlock()`](api.md) method is deprecated. Use `blocks.insert()` instead.
- `Fix` — Resolve bug with deleting leading new lines [#726](https://github.com/codex-team/editor.js/issues/726)
- `Fix` — Fix inline link Tool to support different link types like `mailto` and `tel` [#809](https://github.com/codex-team/editor.js/issues/809)
- `Fix` — Added `typeof` util method to check exact object type [#805](https://github.com/codex-team/editor.js/issues/805)
Expand Down
11 changes: 11 additions & 0 deletions docs/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ onPaste (event) {
}
```

### Disable paste handling

If you need to disable paste handling on your Tool for some reason, you can provide `false` as `pasteConfig` value.
That way paste event won't be processed if fired on your Tool:

```javascript
static get pasteConfig {
return false;
}
```

## Sanitize <a name="sanitize"></a>

Editor.js provides [API](sanitizer.md) to clean taint strings.
Expand Down
26 changes: 22 additions & 4 deletions src/components/modules/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export default class Paste extends Module {
[tool: string]: FilesSubstitution,
} = {};

/**
* List of tools which do not need a paste handling
*/
private exceptionList: string[] = [];

/**
* Set onPaste callback and collect tools` paste configurations
*
Expand Down Expand Up @@ -247,7 +252,12 @@ export default class Paste extends Module {
data: {},
}) as BlockTool;

if (!toolInstance.onPaste || typeof toolInstance.onPaste !== 'function') {
if (tool.pasteConfig === false) {
this.exceptionList.push(name);
return;
}

if (typeof toolInstance.onPaste !== 'function') {
return;
}

Expand Down Expand Up @@ -378,15 +388,23 @@ export default class Paste extends Module {
* @param {ClipboardEvent} event
*/
private handlePasteEvent = async (event: ClipboardEvent): Promise<void> => {
const {BlockManager, Tools, Toolbar} = this.Editor;
const {BlockManager, Toolbar} = this.Editor;

/** If target is native input or is not Block, use browser behaviour */
if (
!BlockManager.currentBlock || this.isNativeBehaviour(event.target) && !event.clipboardData.types.includes('Files')
!BlockManager.currentBlock ||
this.isNativeBehaviour(event.target) && !event.clipboardData.types.includes('Files')
) {
return;
}

/**
* If Tools is in list of exceptions, skip processing of paste event
*/
if (BlockManager.currentBlock && this.exceptionList.includes(BlockManager.currentBlock.name)) {
return;
}

event.preventDefault();
this.processDataTransfer(event.clipboardData);

Expand Down Expand Up @@ -496,7 +514,7 @@ export default class Paste extends Module {
break;
}

const {tags} = Tools.blockTools[tool].pasteConfig;
const {tags} = Tools.blockTools[tool].pasteConfig as PasteConfig;

const toolTags = tags.reduce((result, tag) => {
result[tag.toLowerCase()] = {};
Expand Down
2 changes: 1 addition & 1 deletion types/tools/block-tool.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface BlockToolConstructable extends BaseToolConstructable {
/**
* Paste substitutions configuration
*/
pasteConfig?: PasteConfig;
pasteConfig?: PasteConfig | false;

/**
* Rules that specified how this Tool can be converted into/from another Tool
Expand Down

0 comments on commit a923846

Please sign in to comment.