chore(core): 调整添加menu时的对应参数设置

This commit is contained in:
王立帮
2025-05-18 00:11:11 +08:00
parent fb687bed1f
commit ac6af68999
10 changed files with 68 additions and 62 deletions

View File

@@ -39,16 +39,6 @@ const { BOARD, USER, SELECTED_BOARD } = Config;
/**
* INFO = {
* "boardName": {
* "key": string | null,
* "config": object | null,
* "default": object | null
* }
* }
**/
Boards.INFO = {};
Boards.NAME = []; Boards.NAME = [];

View File

@@ -35,7 +35,7 @@ class ContextMenu {
if (item.children) { if (item.children) {
item.data.items = this.generate(item.children); item.data.items = this.generate(item.children);
} }
menuItems[item.type] = item.data; menuItems[item.id] = item.data;
} }
return menuItems; return menuItems;
} }

View File

@@ -45,11 +45,13 @@ class EditorCode extends EditorMonaco {
} }
#addContextMenu_() { #addContextMenu_() {
this.#contextMenu_ = new ContextMenu(`div[page-id="${this.getId()}"]`); this.#contextMenu_ = new ContextMenu(`div[page-id="${this.getId()}"]`, {
zIndex: 300
});
let menu = new Menu(); let menu = new Menu();
menu.add({ menu.add({
weight: 0, weight: 0,
type: 'cut', id: 'cut',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.cut'], 'Ctrl+X'), name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.cut'], 'Ctrl+X'),
@@ -58,7 +60,7 @@ class EditorCode extends EditorMonaco {
}); });
menu.add({ menu.add({
weight: 1, weight: 1,
type: 'copy', id: 'copy',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.copy'], 'Ctrl+C'), name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.copy'], 'Ctrl+C'),
@@ -67,7 +69,7 @@ class EditorCode extends EditorMonaco {
}); });
menu.add({ menu.add({
weight: 2, weight: 2,
type: 'paste', id: 'paste',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.paste'], 'Ctrl+V'), name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.paste'], 'Ctrl+V'),
@@ -76,12 +78,12 @@ class EditorCode extends EditorMonaco {
}); });
menu.add({ menu.add({
weight: 3, weight: 3,
type: 'sep1', id: 'sep1',
data: '---------' data: '---------'
}); });
menu.add({ menu.add({
weight: 4, weight: 4,
type: 'togglecomment', id: 'togglecomment',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.togglecomment'], 'Ctrl+/'), name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.togglecomment'], 'Ctrl+/'),
@@ -90,7 +92,7 @@ class EditorCode extends EditorMonaco {
}); });
menu.add({ menu.add({
weight: 5, weight: 5,
type: 'toggleBlockComment', id: 'toggleBlockComment',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.toggleBlockComment'], 'Shift+Alt+A'), name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.toggleBlockComment'], 'Shift+Alt+A'),

View File

@@ -94,12 +94,12 @@ class EditorMix extends EditorBase {
let codeMenu = contextMenu.getItem('code'); let codeMenu = contextMenu.getItem('code');
codeMenu.add({ codeMenu.add({
weight: 6, weight: 6,
type: 'sep2', id: 'sep2',
data: '---------' data: '---------'
}); });
codeMenu.add({ codeMenu.add({
weight: 7, weight: 7,
type: 'block', id: 'block',
data: { data: {
isHtmlName: false, isHtmlName: false,
name: Msg.Lang['editor.contextMenu.exitCodeEditor'], name: Msg.Lang['editor.contextMenu.exitCodeEditor'],
@@ -109,7 +109,7 @@ class EditorMix extends EditorBase {
let blockMenu = new Menu(); let blockMenu = new Menu();
blockMenu.add({ blockMenu.add({
weight: 0, weight: 0,
type: 'copy', id: 'copy',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.copy'], 'Ctrl+C'), name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.copy'], 'Ctrl+C'),
@@ -118,12 +118,12 @@ class EditorMix extends EditorBase {
}); });
blockMenu.add({ blockMenu.add({
weight: 1, weight: 1,
type: 'sep1', id: 'sep1',
data: '---------' data: '---------'
}); });
blockMenu.add({ blockMenu.add({
weight: 2, weight: 2,
type: 'code', id: 'code',
data: { data: {
isHtmlName: false, isHtmlName: false,
name: Msg.Lang['editor.contextMenu.enterCodeEditor'], name: Msg.Lang['editor.contextMenu.enterCodeEditor'],

View File

@@ -250,7 +250,7 @@ class FileTree extends Component {
let menu = new Menu(); let menu = new Menu();
menu.add({ menu.add({
weight: 0, weight: 0,
type: 'new_folder', id: 'new_folder',
preconditionFn: ($trigger) => { preconditionFn: ($trigger) => {
let type = $trigger.attr('type'); let type = $trigger.attr('type');
return ['root', 'folder'].includes(type); return ['root', 'folder'].includes(type);
@@ -272,7 +272,7 @@ class FileTree extends Component {
}); });
menu.add({ menu.add({
weight: 1, weight: 1,
type: 'new_file', id: 'new_file',
preconditionFn: ($trigger) => { preconditionFn: ($trigger) => {
let type = $trigger.attr('type'); let type = $trigger.attr('type');
return ['root', 'folder'].includes(type); return ['root', 'folder'].includes(type);
@@ -294,7 +294,7 @@ class FileTree extends Component {
}); });
menu.add({ menu.add({
weight: 2, weight: 2,
type: 'sep1', id: 'sep1',
preconditionFn: ($trigger) => { preconditionFn: ($trigger) => {
let type = $trigger.attr('type'); let type = $trigger.attr('type');
return ['folder'].includes(type); return ['folder'].includes(type);
@@ -303,7 +303,7 @@ class FileTree extends Component {
}); });
menu.add({ menu.add({
weight: 3, weight: 3,
type: 'cut', id: 'cut',
preconditionFn: ($trigger) => { preconditionFn: ($trigger) => {
let type = $trigger.attr('type'); let type = $trigger.attr('type');
return ['file', 'folder'].includes(type); return ['file', 'folder'].includes(type);
@@ -319,7 +319,7 @@ class FileTree extends Component {
}); });
menu.add({ menu.add({
weight: 4, weight: 4,
type: 'copy', id: 'copy',
preconditionFn: ($trigger) => { preconditionFn: ($trigger) => {
let type = $trigger.attr('type'); let type = $trigger.attr('type');
return ['file', 'folder'].includes(type); return ['file', 'folder'].includes(type);
@@ -335,7 +335,7 @@ class FileTree extends Component {
}); });
menu.add({ menu.add({
weight: 5, weight: 5,
type: 'paste', id: 'paste',
preconditionFn: ($trigger) => { preconditionFn: ($trigger) => {
let type = $trigger.attr('type'); let type = $trigger.attr('type');
return ['folder'].includes(type); return ['folder'].includes(type);
@@ -351,12 +351,12 @@ class FileTree extends Component {
}); });
menu.add({ menu.add({
weight: 6, weight: 6,
type: 'sep2', id: 'sep2',
data: '---------' data: '---------'
}); });
menu.add({ menu.add({
weight: 7, weight: 7,
type: 'copy_path', id: 'copy_path',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['fileTree.copyPath'], ''), name: ContextMenu.getItem(Msg.Lang['fileTree.copyPath'], ''),
@@ -375,7 +375,7 @@ class FileTree extends Component {
}); });
menu.add({ menu.add({
weight: 8, weight: 8,
type: 'rename', id: 'rename',
preconditionFn: ($trigger) => { preconditionFn: ($trigger) => {
let type = $trigger.attr('type'); let type = $trigger.attr('type');
return ['file', 'folder'].includes(type); return ['file', 'folder'].includes(type);
@@ -396,7 +396,7 @@ class FileTree extends Component {
}); });
menu.add({ menu.add({
weight: 9, weight: 9,
type: 'del', id: 'del',
preconditionFn: ($trigger) => { preconditionFn: ($trigger) => {
let type = $trigger.attr('type'); let type = $trigger.attr('type');
return ['file', 'folder'].includes(type); return ['file', 'folder'].includes(type);

View File

@@ -1,6 +1,7 @@
goog.loadJs('common', () => { goog.loadJs('common', () => {
goog.require('path'); goog.require('path');
goog.require('tippy');
goog.require('Blockly'); goog.require('Blockly');
goog.require('layui'); goog.require('layui');
goog.require('$.select2'); goog.require('$.select2');

View File

@@ -45,7 +45,7 @@ class Menu {
break; break;
} }
this.#menuItems_.splice(i, 0, item); this.#menuItems_.splice(i, 0, item);
this.#ids_[id] = true; this.#ids_[id] = item;
return id; return id;
} }
@@ -68,13 +68,23 @@ class Menu {
this.#menuItems_ = []; this.#menuItems_ = [];
} }
hasKey(id) {
return !!this.#ids_[id];
}
getItem(id) { getItem(id) {
return this.#ids_[id] ?? null; return this.#ids_[id] ?? null;
} }
getAllItems() { getAllItems() {
if (this.#isDynamic_) { if (this.#isDynamic_) {
this.runEvent('onRead'); this.empty();
const results = this.runEvent('onRead');
if (results?.length) {
for (let item of results[0]) {
this.add(item);
}
}
} }
return this.#menuItems_; return this.#menuItems_;
} }

View File

@@ -141,7 +141,7 @@ class StatusBarAmpy extends PageBase {
fileTreeMenu.add({ fileTreeMenu.add({
weight: 14, weight: 14,
type: 'sep5', id: 'sep5',
preconditionFn: ($trigger) => { preconditionFn: ($trigger) => {
const selectedNodeId = this.#fileTree_.getSelectedNodeId(); const selectedNodeId = this.#fileTree_.getSelectedNodeId();
let type = $trigger.attr('type'); let type = $trigger.attr('type');
@@ -156,7 +156,7 @@ class StatusBarAmpy extends PageBase {
fileTreeMenu.add({ fileTreeMenu.add({
weight: 15, weight: 15,
type: 'refresh', id: 'refresh',
preconditionFn: ($trigger) => { preconditionFn: ($trigger) => {
const selectedNodeId = this.#fileTree_.getSelectedNodeId(); const selectedNodeId = this.#fileTree_.getSelectedNodeId();
let type = $trigger.attr('type'); let type = $trigger.attr('type');
@@ -188,7 +188,7 @@ class StatusBarAmpy extends PageBase {
fileTreeMenu.add({ fileTreeMenu.add({
weight: 16, weight: 16,
type: 'sep6', id: 'sep6',
preconditionFn: ($trigger) => { preconditionFn: ($trigger) => {
let type = $trigger.attr('type'); let type = $trigger.attr('type');
return ['root'].includes(type); return ['root'].includes(type);
@@ -198,7 +198,7 @@ class StatusBarAmpy extends PageBase {
fileTreeMenu.add({ fileTreeMenu.add({
weight: 17, weight: 17,
type: 'exit', id: 'exit',
preconditionFn: ($trigger) => { preconditionFn: ($trigger) => {
let type = $trigger.attr('type'); let type = $trigger.attr('type');
return ['root'].includes(type); return ['root'].includes(type);
@@ -224,7 +224,7 @@ class StatusBarAmpy extends PageBase {
editorMenu.add({ editorMenu.add({
weight: 0, weight: 0,
type: 'cut', id: 'cut',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.cut'], 'Ctrl+X'), name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.cut'], 'Ctrl+X'),
@@ -233,7 +233,7 @@ class StatusBarAmpy extends PageBase {
}); });
editorMenu.add({ editorMenu.add({
weight: 1, weight: 1,
type: 'copy', id: 'copy',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.copy'], 'Ctrl+C'), name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.copy'], 'Ctrl+C'),
@@ -242,7 +242,7 @@ class StatusBarAmpy extends PageBase {
}); });
editorMenu.add({ editorMenu.add({
weight: 2, weight: 2,
type: 'paste', id: 'paste',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.paste'], 'Ctrl+V'), name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.paste'], 'Ctrl+V'),
@@ -251,12 +251,12 @@ class StatusBarAmpy extends PageBase {
}); });
editorMenu.add({ editorMenu.add({
weight: 3, weight: 3,
type: 'sep1', id: 'sep1',
data: '---------' data: '---------'
}); });
editorMenu.add({ editorMenu.add({
weight: 4, weight: 4,
type: 'togglecomment', id: 'togglecomment',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.togglecomment'], 'Ctrl+/'), name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.togglecomment'], 'Ctrl+/'),
@@ -265,7 +265,7 @@ class StatusBarAmpy extends PageBase {
}); });
/*editorMenu.add({ /*editorMenu.add({
weight: 5, weight: 5,
type: 'toggleBlockComment', id: 'toggleBlockComment',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.toggleBlockComment'], 'Shift+Alt+A'), name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.toggleBlockComment'], 'Shift+Alt+A'),
@@ -275,7 +275,7 @@ class StatusBarAmpy extends PageBase {
editorMenu.add({ editorMenu.add({
weight: 6, weight: 6,
type: 'sep2', id: 'sep2',
preconditionFn: () => { preconditionFn: () => {
return this.#changed_; return this.#changed_;
}, },
@@ -284,7 +284,7 @@ class StatusBarAmpy extends PageBase {
editorMenu.add({ editorMenu.add({
weight: 7, weight: 7,
type: 'save', id: 'save',
preconditionFn: () => { preconditionFn: () => {
return this.#changed_; return this.#changed_;
}, },

View File

@@ -35,11 +35,13 @@ class StatusBar extends EditorAce {
} }
#addContextMenu_() { #addContextMenu_() {
this.#contextMenu_ = new ContextMenu(`div[page-id="${this.getId()}"]`); this.#contextMenu_ = new ContextMenu(`div[page-id="${this.getId()}"]`, {
zIndex: 300
});
let menu = new Menu(); let menu = new Menu();
menu.add({ menu.add({
weight: 0, weight: 0,
type: 'copy', id: 'copy',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.copy'], 'Ctrl+C'), name: ContextMenu.getItem(Msg.Lang['editor.contextMenu.copy'], 'Ctrl+C'),

View File

@@ -151,7 +151,7 @@ class StatusBarsManager extends PagesManager {
let serialChildMenu = new Menu(true); let serialChildMenu = new Menu(true);
menu.add({ menu.add({
weight: 0, weight: 0,
type: 'serial-default', id: 'serial-default',
preconditionFn: () => { preconditionFn: () => {
return !!Serial.getCurrentPortsName().length; return !!Serial.getCurrentPortsName().length;
}, },
@@ -165,7 +165,7 @@ class StatusBarsManager extends PagesManager {
}); });
menu.add({ menu.add({
weight: 1, weight: 1,
type: 'serial', id: 'serial',
children: serialChildMenu, children: serialChildMenu,
data: { data: {
isHtmlName: true, isHtmlName: true,
@@ -175,7 +175,7 @@ class StatusBarsManager extends PagesManager {
/*menu.add({ /*menu.add({
weight: 2, weight: 2,
type: 'lib', id: 'lib',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem('第三方库管理', ''), name: ContextMenu.getItem('第三方库管理', ''),
@@ -190,19 +190,19 @@ class StatusBarsManager extends PagesManager {
&& !['BBC micro:bit', 'Mithon CC'].includes(BOARD.boardType)) { && !['BBC micro:bit', 'Mithon CC'].includes(BOARD.boardType)) {
menu.add({ menu.add({
weight: 2, weight: 2,
type: 'sep1', id: 'sep1',
data: '---------' data: '---------'
}); });
menu.add({ menu.add({
weight: 3, weight: 3,
type: 'ampy', id: 'ampy',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: ContextMenu.getItem(Msg.Lang['statusbar.ampy'], ''), name: ContextMenu.getItem(Msg.Lang['statusbar.ampy'], ''),
callback: (key, opt) => { callback: (key, opt) => {
this.add({ this.add({
type: 'ampy',
id: 'ampy', id: 'ampy',
type: 'ampy',
name: Msg.Lang['statusbar.ampy'], name: Msg.Lang['statusbar.ampy'],
title: Msg.Lang['statusbar.ampy'] title: Msg.Lang['statusbar.ampy']
}); });
@@ -212,14 +212,14 @@ class StatusBarsManager extends PagesManager {
}); });
} }
serialChildMenu.bind('onRead', () => { serialChildMenu.bind('onRead', () => {
let options = this.#getMenu_() ?? {}; const options = this.#getMenu_() ?? {};
options.list = options.list ?? []; options.list = options.list ?? [];
options.empty = options.empty ?? Msg.Lang['statusbar.serial.noPort']; options.empty = options.empty ?? Msg.Lang['statusbar.serial.noPort'];
serialChildMenu.empty(); const result = [];
if (!options.list.length) { if (!options.list.length) {
serialChildMenu.add({ result.push({
weight: 1, weight: 1,
type: 'empty', id: 'empty',
data: { data: {
isHtmlName: true, isHtmlName: true,
name: options.empty, name: options.empty,
@@ -228,9 +228,9 @@ class StatusBarsManager extends PagesManager {
}); });
} }
for (let i in options.list) { for (let i in options.list) {
serialChildMenu.add({ result.push({
weight: 1, weight: 1,
type: `serial${i}`, id: `serial${i}`,
data: { data: {
isHtmlName: true, isHtmlName: true,
name: options.list[i], name: options.list[i],
@@ -238,6 +238,7 @@ class StatusBarsManager extends PagesManager {
} }
}); });
} }
return result;
}); });
this.#dropdownMenu_ = new DropdownMenu(selector); this.#dropdownMenu_ = new DropdownMenu(selector);
this.#dropdownMenu_.register('menu', menu); this.#dropdownMenu_.register('menu', menu);