blockbench/js/copy_paste.js

228 lines
5.4 KiB
JavaScript
Raw Normal View History

2019-12-16 03:04:31 +08:00
const Clipbench = {
elements: [],
2020-03-05 03:56:17 +08:00
types: {
text: 'text',
face_dialog: 'face_dialog',
display_slot: 'display_slot',
keyframe: 'keyframe',
face: 'face',
texture: 'texture',
outliner: 'outliner',
texture_selection: 'texture_selection',
},
getCopyType(mode, check) {
// mode: 1 = copy, 2 = paste
let p = Prop.active_panel;
let text;
if (!check) {
text = window.getSelection()+'';
}
2019-12-16 03:04:31 +08:00
if (text) {
2020-03-05 03:56:17 +08:00
return Clipbench.types.text;
}
if (Painter.selection.canvas && Toolbox.selected.id == 'copy_paste_tool') {
return Clipbench.types.texture_selection;
2019-12-16 03:04:31 +08:00
}
if (open_dialog == 'uv_dialog') {
2020-03-05 03:56:17 +08:00
return Clipbench.types.face_dialog
2019-12-16 03:04:31 +08:00
}
if (display_mode) {
2020-03-05 03:56:17 +08:00
return Clipbench.types.display_slot
2019-12-16 03:04:31 +08:00
}
2020-03-11 05:19:17 +08:00
if (Animator.open && Timeline.animators.length && (Timeline.selected.length || mode === 2) && ['keyframe', 'timeline'].includes(p)) {
2020-03-05 03:56:17 +08:00
return Clipbench.types.keyframe
2019-12-16 03:04:31 +08:00
}
if ((p == 'uv' || p == 'preview') && Modes.edit) {
2020-03-05 03:56:17 +08:00
return Clipbench.types.face;
2019-12-16 03:04:31 +08:00
}
2020-07-16 15:32:59 +08:00
if (p == 'textures' && isApp && (Texture.selected || mode === 2)) {
2020-03-05 03:56:17 +08:00
return Clipbench.types.texture;
2019-12-16 03:04:31 +08:00
}
if (p == 'outliner' && Modes.edit) {
2020-03-05 03:56:17 +08:00
return Clipbench.types.outliner;
2019-12-16 03:04:31 +08:00
}
},
copy(event, cut) {
2020-03-05 03:56:17 +08:00
switch (Clipbench.getCopyType(1)) {
2019-12-16 03:04:31 +08:00
case 'text':
Clipbench.setText(window.getSelection()+'');
break;
case 'face_dialog':
uv_dialog.copy(event);
break;
case 'display_slot':
DisplayMode.copy();
break;
case 'keyframe':
if (Timeline.selected.length) {
Clipbench.setKeyframes();
if (cut) {
BarItems.delete.trigger();
}
}
break;
case 'face':
main_uv.copy(event);
break;
case 'texture':
2020-07-16 15:32:59 +08:00
Clipbench.setTexture(Texture.selected);
2019-12-16 03:04:31 +08:00
if (cut) {
BarItems.delete.trigger();
}
break;
case 'outliner':
Clipbench.setElements();
Clipbench.setGroup();
if (Group.selected) {
Clipbench.setGroup(Group.selected);
} else {
Clipbench.setElements(selected);
}
if (cut) {
BarItems.delete.trigger();
}
break;
}
},
paste(event) {
2020-03-05 03:56:17 +08:00
switch (Clipbench.getCopyType(2)) {
2019-12-16 03:04:31 +08:00
case 'text':
Clipbench.setText(window.getSelection()+'');
break;
2020-03-05 03:56:17 +08:00
case 'texture_selection':
main_uv.addPastingOverlay();
break;
2019-12-16 03:04:31 +08:00
case 'face_dialog':
uv_dialog.paste(event)
break;
case 'display_slot':
DisplayMode.paste();
break;
case 'keyframe':
Clipbench.pasteKeyframes()
break;
case 'face':
main_uv.paste(event);
break;
case 'texture':
Clipbench.pasteTextures();
break;
case 'outliner':
Clipbench.pasteOutliner(event);
break;
}
},
setGroup(group) {
if (!group) {
Clipbench.group = undefined
return;
}
Clipbench.group = group.getSaveCopy()
if (isApp) {
clipboard.writeHTML(JSON.stringify({type: 'group', content: Clipbench.group}))
}
},
setElements(arr) {
if (!arr) {
Clipbench.elements = []
return;
}
arr.forEach(function(obj) {
Clipbench.elements.push(obj.getSaveCopy())
})
if (isApp) {
clipboard.writeHTML(JSON.stringify({type: 'elements', content: Clipbench.elements}))
}
},
setText(text) {
if (isApp) {
clipboard.writeText(text)
} else {
document.execCommand('copy')
}
},
pasteOutliner(event) {
Undo.initEdit({outliner: true, elements: [], selection: true});
//Group
var target = 'root'
if (Group.selected) {
target = Group.selected
Group.selected.isOpen = true
} else if (selected[0]) {
target = selected[0]
}
selected.length = 0
if (isApp) {
var raw = clipboard.readHTML()
try {
var data = JSON.parse(raw)
if (data.type === 'elements' && data.content) {
Clipbench.group = undefined;
Clipbench.elements = data.content;
} else if (data.type === 'group' && data.content) {
Clipbench.group = data.content;
Clipbench.elements = [];
}
} catch (err) {}
}
if (Clipbench.group) {
function iterate(obj, parent) {
if (obj.children) {
var copy = new Group(obj).addTo(parent).init()
2020-01-24 01:53:36 +08:00
copy.createUniqueName();
2019-12-16 03:04:31 +08:00
if (obj.children && obj.children.length) {
obj.children.forEach((child) => {
iterate(child, copy)
})
}
} else {
var el = NonGroup.fromSave(obj).addTo(parent).selectLow();
2020-01-24 01:53:36 +08:00
el.createUniqueName();
if (el instanceof Cube) {
Canvas.adaptObjectPosition(el);
}
2019-12-16 03:04:31 +08:00
}
}
iterate(Clipbench.group, target)
updateSelection()
} else if (Clipbench.elements && Clipbench.elements.length) {
Clipbench.elements.forEach(function(obj) {
2020-01-24 01:53:36 +08:00
var el = NonGroup.fromSave(obj).addTo(target).selectLow();
el.createUniqueName();
2019-12-16 03:04:31 +08:00
})
2020-01-24 01:53:36 +08:00
Canvas.updatePositions();
2019-12-16 03:04:31 +08:00
}
Undo.finishEdit('paste', {outliner: true, elements: selected, selection: true});
}
}
BARS.defineActions(function() {
new Action('copy', {
icon: 'fa-copy',
category: 'edit',
work_in_dialog: true,
2020-03-05 03:56:17 +08:00
condition: () => Clipbench.getCopyType(1, true),
2019-12-16 03:04:31 +08:00
keybind: new Keybind({key: 67, ctrl: true, shift: null}),
click: function (event) {Clipbench.copy(event)}
})
new Action('cut', {
icon: 'fa-cut',
category: 'edit',
work_in_dialog: true,
2020-03-05 03:56:17 +08:00
condition: () => Clipbench.getCopyType(1, true),
2019-12-16 03:04:31 +08:00
keybind: new Keybind({key: 88, ctrl: true, shift: null}),
click: function (event) {Clipbench.copy(event, true)}
})
new Action('paste', {
icon: 'fa-clipboard',
category: 'edit',
work_in_dialog: true,
2020-03-05 03:56:17 +08:00
condition: () => Clipbench.getCopyType(2, true),
2019-12-16 03:04:31 +08:00
keybind: new Keybind({key: 86, ctrl: true, shift: null}),
click: function (event) {Clipbench.paste(event)}
})
})