refactor: code of copy

This commit is contained in:
QiShaoXuan 2022-08-18 13:57:58 +08:00
parent 3c8b04d91a
commit 16a6cca015
3 changed files with 19 additions and 49 deletions

View File

@ -70,12 +70,15 @@ const AffineBoard = ({
set_app(app); set_app(app);
}, },
async onCopy(e, groupIds) { async onCopy(e, groupIds) {
const [mimeType, data] = await getClipDataOfBlocksById( const clip = await getClipDataOfBlocksById(
editor, editor,
groupIds groupIds
); );
e.clipboardData?.setData(mimeType, data); e.clipboardData?.setData(
clip.getMimeType(),
clip.getData()
);
}, },
onChangePage(app, shapes, bindings, assets) { onChangePage(app, shapes, bindings, assets) {
Promise.all( Promise.all(

View File

@ -1,13 +1,9 @@
import { Editor } from '../editor'; import { Editor } from '../editor';
import { SelectInfo, SelectBlock } from '../selection'; import { SelectInfo } from '../selection';
import { import { OFFICE_CLIPBOARD_MIMETYPE } from './types';
ClipBlockInfo,
OFFICE_CLIPBOARD_MIMETYPE,
InnerClipInfo,
} from './types';
import { Clip } from './clip'; import { Clip } from './clip';
import assert from 'assert';
import ClipboardParse from './clipboard-parse'; import ClipboardParse from './clipboard-parse';
import { getClipDataOfBlocksById } from './utils';
class Copy { class Copy {
private _editor: Editor; private _editor: Editor;
@ -47,16 +43,11 @@ class Copy {
} }
async getClips() { async getClips() {
const clips: any[] = []; const clips: Clip[] = [];
// get custom clip // get custom clip
const affineClip = await this._getAffineClip(); const affineClip = await this._getAffineClip();
clips.push( clips.push(affineClip);
new Clip(
OFFICE_CLIPBOARD_MIMETYPE.DOCS_DOCUMENT_SLICE_CLIP_WRAPPED,
JSON.stringify(affineClip)
)
);
// get common html clip // get common html clip
const htmlClip = await this._clipboardParse.generateHtml(); const htmlClip = await this._clipboardParse.generateHtml();
@ -66,39 +57,14 @@ class Copy {
return clips; return clips;
} }
private async _getAffineClip(): Promise<InnerClipInfo> { private async _getAffineClip(): Promise<Clip> {
const clips: ClipBlockInfo[] = [];
const selectInfo: SelectInfo = const selectInfo: SelectInfo =
await this._editor.selectionManager.getSelectInfo(); await this._editor.selectionManager.getSelectInfo();
for (let i = 0; i < selectInfo.blocks.length; i++) {
const selBlock = selectInfo.blocks[i];
const clipBlockInfo = await this._getClipInfoOfBlock(selBlock);
clips.push(clipBlockInfo);
}
return {
select: selectInfo,
data: clips,
};
}
private async _getClipInfoOfBlock(selBlock: SelectBlock) { return getClipDataOfBlocksById(
const block = await this._editor.getBlockById(selBlock.blockId); this._editor,
const blockView = this._editor.getView(block.type); selectInfo.blocks.map(block => block.blockId)
assert(blockView);
const blockInfo: ClipBlockInfo = {
type: block.type,
properties: blockView.getSelProperties(block, selBlock),
children: [] as any[],
};
for (let i = 0; i < selBlock.children.length; i++) {
const childInfo = await this._getClipInfoOfBlock(
selBlock.children[i]
); );
blockInfo.children.push(childInfo);
}
return blockInfo;
} }
private _copyToClipboardFromPc(clips: any[]) { private _copyToClipboardFromPc(clips: any[]) {

View File

@ -42,10 +42,11 @@ export const getClipDataOfBlocksById = async (
const clipInfos = await Promise.all( const clipInfos = await Promise.all(
blockIds.map(blockId => getClipInfoOfBlockById(editor, blockId)) blockIds.map(blockId => getClipInfoOfBlockById(editor, blockId))
); );
return [
return new Clip(
OFFICE_CLIPBOARD_MIMETYPE.DOCS_DOCUMENT_SLICE_CLIP_WRAPPED, OFFICE_CLIPBOARD_MIMETYPE.DOCS_DOCUMENT_SLICE_CLIP_WRAPPED,
JSON.stringify({ JSON.stringify({
data: clipInfos, data: clipInfos,
}), })
]; );
}; };