Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Option to disable paste handling #813

Merged
merged 3 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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