Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 1x 1x 1x 1x 1x 166x 166x 166x 166x 1x 170x 170x 170x 8x 10x 6x 1x 93x 93x 93x 93x 93x 1x 170x 1x 16x 16x 16x 28x 14x 14x 14x 14x 6x 6x 4x 6x 166x 8x 8x 8x 8x 8x 8x 8x 7x 1x 24x 1x 14x 2x 12x 9x 3x 3x 3x 3x 3x 1x 166x | import { deleteEmptyList } from './utils/deleteEmptyList';
import { getContentForCopy } from '../../command/cutCopy/getContentForCopy';
import { paste } from '../../command/paste/paste';
import {
ChangeSource,
deleteSelection,
extractClipboardItems,
normalizeContentModel,
toArray,
} from 'roosterjs-content-model-dom';
import type {
ClipboardData,
CopyPastePluginState,
IEditor,
EditorOptions,
PluginWithState,
} from 'roosterjs-content-model-types';
/**
* Copy and paste plugin for handling onCopy and onPaste event
*/
class CopyPastePlugin implements PluginWithState<CopyPastePluginState> {
private editor: IEditor | null = null;
private disposer: (() => void) | null = null;
private state: CopyPastePluginState;
/**
* Construct a new instance of CopyPastePlugin
* @param option The editor option
*/
constructor(option: EditorOptions) {
this.state = {
allowedCustomPasteType: option.allowedCustomPasteType || [],
tempDiv: null,
defaultPasteType: option.defaultPasteType,
};
}
/**
* Get a friendly name of this plugin
*/
getName() {
return 'CopyPaste';
}
/**
* Initialize this plugin. This should only be called from Editor
* @param editor Editor instance
*/
initialize(editor: IEditor) {
this.editor = editor;
this.disposer = this.editor.attachDomEvent({
paste: {
beforeDispatch: e => this.onPaste(e),
},
copy: {
beforeDispatch: e => this.onCutCopy(e, false /*isCut*/),
},
cut: {
beforeDispatch: e => this.onCutCopy(e, true /*isCut*/),
},
});
}
/**
* Dispose this plugin
*/
dispose() {
Iif (this.state.tempDiv) {
this.state.tempDiv.parentNode?.removeChild(this.state.tempDiv);
this.state.tempDiv = null;
}
Eif (this.disposer) {
this.disposer();
}
this.disposer = null;
this.editor = null;
}
/**
* Get plugin state object
*/
getState() {
return this.state;
}
private onCutCopy(event: Event, isCut: boolean) {
Iif (!this.editor || !isClipboardEvent(event)) {
return;
}
const textAndHtmlContent = getContentForCopy(this.editor, isCut, event);
if (textAndHtmlContent) {
const { htmlContent, textContent } = textAndHtmlContent;
event.preventDefault();
event.clipboardData?.setData('text/html', htmlContent.innerHTML);
event.clipboardData?.setData('text/plain', textContent);
if (isCut) {
this.editor.formatContentModel(
(model, context) => {
if (
deleteSelection(model, [deleteEmptyList], context).deleteResult ==
'range'
) {
normalizeContentModel(model);
}
return true;
},
{
apiName: 'cut',
changeSource: ChangeSource.Cut,
}
);
}
}
}
private onPaste = (event: Event) => {
Eif (this.editor && isClipboardEvent(event)) {
const editor = this.editor;
const dataTransfer = event.clipboardData;
Eif (shouldPreventDefaultPaste(dataTransfer, editor)) {
event.preventDefault();
extractClipboardItems(
toArray(dataTransfer!.items),
this.state.allowedCustomPasteType
).then((clipboardData: ClipboardData) => {
if (!editor.isDisposed()) {
paste(editor, clipboardData, this.state.defaultPasteType);
}
});
}
}
};
}
function isClipboardEvent(event: Event): event is ClipboardEvent {
return !!(event as ClipboardEvent).clipboardData;
}
/**
* @internal
* Exported only for unit testing
*/
export function shouldPreventDefaultPaste(
dataTransfer: DataTransfer | null,
editor: IEditor
): boolean {
if (!dataTransfer?.items) {
return false;
}
if (!editor.getEnvironment().isAndroid) {
return true;
}
// On Android, the clipboard data from Office apps is a file, which can't be loaded
// so we have to allow the default browser behavior
return toArray(dataTransfer.items).some(item => {
const { type } = item;
const isNormalFile = item.kind === 'file' && type !== '';
const isText = type.indexOf('text/') === 0;
return isNormalFile || isText;
});
}
/**
* @internal
* Create a new instance of CopyPastePlugin
* @param option The editor option
*/
export function createCopyPastePlugin(
option: EditorOptions
): PluginWithState<CopyPastePluginState> {
return new CopyPastePlugin(option);
}
|