| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- (() => {
- /**
- * 统一管理素材管理的键盘快捷键(复制自 client/js/disk/shortcut-keys.js)
- */
- class ResourceManagerShortcutKeys {
- constructor(options = {}) {
- this.selection = options.selection || null;
- this.onDelete = options.onDelete || null;
- this.onRename = options.onRename || null;
- this.onCopy = options.onCopy || null;
- this.onCut = options.onCut || null;
- this.onPaste = options.onPaste || null;
- this.handleKeyDown = this.handleKeyDown.bind(this);
- document.addEventListener('keydown', this.handleKeyDown);
-
- // 确保 iframe 可以接收键盘事件
- window.focus();
- console.log('[ShortcutKeys] 快捷键模块已初始化');
- }
- destroy() {
- document.removeEventListener('keydown', this.handleKeyDown);
- }
- handleKeyDown(e) {
- if (this.shouldIgnoreTarget(e.target)) {
- return;
- }
- // Delete
- if (e.key === 'Delete') {
- console.log('[ShortcutKeys] Delete 键被按下');
- console.log('[ShortcutKeys] this.selection:', this.selection);
- console.log('[ShortcutKeys] hasSelection:', this.selection ? this.selection.hasSelection() : 'N/A');
-
- if (this.selection && this.selection.hasSelection()) {
- console.log('[ShortcutKeys] 有选中项,调用 onDelete');
- e.preventDefault();
- this.onDelete && this.onDelete();
- } else {
- console.log('[ShortcutKeys] 没有选中项或 selection 为空');
- }
- return;
- }
- // 重命名
- if (e.key === 'F2') {
- if (this.selection && this.selection.getSelectedCount() === 1) {
- e.preventDefault();
- this.onRename && this.onRename();
- }
- return;
- }
- if (!e.ctrlKey) {
- return;
- }
- const key = e.key.toLowerCase();
- switch (key) {
- case 'a':
- if (this.selection) {
- e.preventDefault();
- this.selection.selectAll();
- }
- break;
- case 'c':
- if (this.selection && this.selection.hasSelection() && this.onCopy) {
- e.preventDefault();
- this.onCopy();
- }
- break;
- case 'x':
- if (this.selection && this.selection.hasSelection() && this.onCut) {
- e.preventDefault();
- this.onCut();
- }
- break;
- case 'v':
- if (this.onPaste) {
- e.preventDefault();
- Promise.resolve(this.onPaste()).catch((error) => {
- console.error('粘贴失败:', error);
- });
- }
- break;
- default:
- break;
- }
- }
- shouldIgnoreTarget(target) {
- if (!target) return false;
- const tag = target.tagName;
- if (!tag) return false;
- if (target.isContentEditable) return true;
- return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT';
- }
- }
- window.ResourceManagerShortcutKeys = ResourceManagerShortcutKeys;
- })();
|