Skip to content

Commit

Permalink
Add handler
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed Dec 30, 2024
1 parent b6b8e03 commit c3ac2b4
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 27 deletions.
5 changes: 4 additions & 1 deletion src/mjs/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

export const CONTENT_GET = 'getContent';
export const CONTEXT_MENU = 'contextMenu';
export const EDIT_HTML = 'editHtml';
export const EDIT_MD = 'editMarkdown';
export const EDIT_TXT = 'editPlainText';
export const EDITOR_CONFIG_GET = 'getEditorConfig';
export const EDITOR_CONFIG_RES = 'resEditorConfig';
export const EDITOR_CONFIG_TS = 'editorConfigTimestamp';
Expand Down Expand Up @@ -57,7 +60,7 @@ export const MODE_EDIT = 'modeEditText';
export const MODE_EDIT_EXT = 'modeEditTextFileExtension';
export const MODE_EDIT_HTML = 'modeEditTextHtml';
export const MODE_EDIT_MD = 'modeEditTextMarkdown';
export const MODE_EDIT_TXT = 'modeEditTextPlaintext';
export const MODE_EDIT_TXT = 'modeEditTextPlainText';
export const MODE_MATHML = 'modeViewMathML';
export const MODE_SELECTION = 'modeViewSelection';
export const MODE_SOURCE = 'modeViewSource';
Expand Down
52 changes: 36 additions & 16 deletions src/mjs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ import {
} from './common.js';
import { setIconBadge } from './icon.js';
import {
CONTENT_GET, CONTEXT_MENU, EDITOR_CONFIG_GET, EDITOR_CONFIG_RES,
EDITOR_CONFIG_TS, EDITOR_EXEC, EDITOR_FILE_NAME, EDITOR_LABEL, EXT_NAME,
FILE_EXT_SELECT, FILE_EXT_SELECT_HTML, FILE_EXT_SELECT_MD,
FILE_EXT_SELECT_TXT, HOST, HOST_COMPAT, HOST_CONNECTION, HOST_ERR_NOTIFY,
HOST_STATUS, HOST_STATUS_GET, HOST_VERSION, HOST_VERSION_CHECK,
HOST_VERSION_LATEST, HOST_VERSION_MIN, ICON, INFO_COLOR, INFO_TEXT,
IS_CONNECTABLE, IS_EXECUTABLE, IS_MAC, LOCAL_FILE_VIEW, MENU_ENABLED,
MODE_EDIT, MODE_EDIT_EXT, MODE_EDIT_HTML, MODE_EDIT_MD, MODE_EDIT_TXT,
MODE_MATHML, MODE_SELECTION, MODE_SOURCE, MODE_SVG, ONLY_EDITABLE,
OPTIONS_OPEN, PATH_OPTIONS_PAGE, PROCESS_CHILD, SYNC_AUTO, SYNC_AUTO_URL,
TMP_FILES_PB, TMP_FILES_PB_REMOVE, TMP_FILE_CREATE, TMP_FILE_DATA_PORT,
TMP_FILE_DATA_REMOVE, TMP_FILE_GET, TMP_FILE_REQ, TMP_FILE_RES, VARS_SET,
WARN_COLOR, WARN_TEXT
CONTENT_GET, CONTEXT_MENU, EDIT_HTML, EDIT_MD, EDIT_TXT, EDITOR_CONFIG_GET,
EDITOR_CONFIG_RES, EDITOR_CONFIG_TS, EDITOR_EXEC, EDITOR_FILE_NAME,
EDITOR_LABEL, EXT_NAME, FILE_EXT_SELECT, FILE_EXT_SELECT_HTML,
FILE_EXT_SELECT_MD, FILE_EXT_SELECT_TXT, HOST, HOST_COMPAT, HOST_CONNECTION,
HOST_ERR_NOTIFY, HOST_STATUS, HOST_STATUS_GET, HOST_VERSION,
HOST_VERSION_CHECK, HOST_VERSION_LATEST, HOST_VERSION_MIN, ICON, INFO_COLOR,
INFO_TEXT, IS_CONNECTABLE, IS_EXECUTABLE, IS_MAC, LOCAL_FILE_VIEW,
MENU_ENABLED, MODE_EDIT, MODE_EDIT_EXT, MODE_EDIT_HTML, MODE_EDIT_MD,
MODE_EDIT_TXT, MODE_MATHML, MODE_SELECTION, MODE_SOURCE, MODE_SVG,
ONLY_EDITABLE, OPTIONS_OPEN, PATH_OPTIONS_PAGE, PROCESS_CHILD, SYNC_AUTO,
SYNC_AUTO_URL, TMP_FILES_PB, TMP_FILES_PB_REMOVE, TMP_FILE_CREATE,
TMP_FILE_DATA_PORT, TMP_FILE_DATA_REMOVE, TMP_FILE_GET, TMP_FILE_REQ,
TMP_FILE_RES, VARS_SET, WARN_COLOR, WARN_TEXT
} from './constant.js';

/* api */
Expand Down Expand Up @@ -900,15 +900,35 @@ export const handleCmd = async (cmd, tab) => {
}
let func;
switch (cmd) {
case EDIT_HTML:
case EDIT_MD:
case EDIT_TXT:
case EDITOR_EXEC: {
if (!tab) {
tab = await getActiveTab();
}
const { id } = tab;
if (tabList.has(id)) {
func = sendMessage(id, {
[CONTENT_GET]: { tab }
});
if (cmd === EDITOR_EXEC) {
func = sendMessage(id, {
[CONTENT_GET]: { tab }
});
} else {
let menuItemId;
if (cmd === EDIT_HTML) {
menuItemId = MODE_EDIT_HTML;
} else if (cmd === EDIT_MD) {
menuItemId = MODE_EDIT_MD;
} else {
menuItemId = MODE_EDIT_TXT;
}
const info = {
menuItemId
};
func = sendMessage(id, {
[CONTENT_GET]: { info, tab }
});
}
}
break;
}
Expand Down
101 changes: 91 additions & 10 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import { browser, mockPort } from './mocha/setup.js';

/* test */
import {
CONTEXT_MENU, EDITOR_CONFIG_GET, EDITOR_CONFIG_RES, EDITOR_EXEC,
EDITOR_FILE_NAME, EDITOR_LABEL, FILE_EXT_SELECT, FILE_EXT_SELECT_HTML,
FILE_EXT_SELECT_MD, FILE_EXT_SELECT_TXT, HOST, HOST_COMPAT, HOST_CONNECTION,
HOST_ERR_NOTIFY, HOST_STATUS_GET, HOST_VERSION, HOST_VERSION_LATEST,
INFO_COLOR, INFO_TEXT, IS_CONNECTABLE, IS_EXECUTABLE, IS_MAC,
LOCAL_FILE_VIEW, MENU_ENABLED, MODE_EDIT, MODE_EDIT_HTML, MODE_EDIT_MD,
MODE_EDIT_TXT, MODE_MATHML, MODE_SELECTION, MODE_SOURCE, MODE_SVG,
ONLY_EDITABLE, OPTIONS_OPEN, PROCESS_CHILD, SYNC_AUTO, SYNC_AUTO_URL,
TMP_FILE_CREATE, TMP_FILE_DATA_PORT, TMP_FILE_DATA_REMOVE, TMP_FILE_GET,
TMP_FILE_RES, WARN_COLOR, WARN_TEXT
CONTEXT_MENU, EDIT_HTML, EDIT_MD, EDIT_TXT, EDITOR_CONFIG_GET,
EDITOR_CONFIG_RES, EDITOR_EXEC, EDITOR_FILE_NAME, EDITOR_LABEL,
FILE_EXT_SELECT, FILE_EXT_SELECT_HTML, FILE_EXT_SELECT_MD,
FILE_EXT_SELECT_TXT, HOST, HOST_COMPAT, HOST_CONNECTION, HOST_ERR_NOTIFY,
HOST_STATUS_GET, HOST_VERSION, HOST_VERSION_LATEST, INFO_COLOR, INFO_TEXT,
IS_CONNECTABLE, IS_EXECUTABLE, IS_MAC, LOCAL_FILE_VIEW, MENU_ENABLED,
MODE_EDIT, MODE_EDIT_HTML, MODE_EDIT_MD, MODE_EDIT_TXT, MODE_MATHML,
MODE_SELECTION, MODE_SOURCE, MODE_SVG, ONLY_EDITABLE, OPTIONS_OPEN,
PROCESS_CHILD, SYNC_AUTO, SYNC_AUTO_URL, TMP_FILE_CREATE, TMP_FILE_DATA_PORT,
TMP_FILE_DATA_REMOVE, TMP_FILE_GET, TMP_FILE_RES, WARN_COLOR, WARN_TEXT
} from '../src/mjs/constant.js';
import * as mjs from '../src/mjs/main.js';

Expand Down Expand Up @@ -2686,6 +2686,87 @@ describe('main', () => {
assert.strictEqual(browser.tabs.query.callCount, j + 1, 'called');
assert.deepEqual(res, {}, 'result');
});

it('should call function', async () => {
const i = browser.tabs.sendMessage.withArgs(2, {
info: {
menuItemId: MODE_EDIT_HTML
},
tab: {
id: 2
}
}).callCount;
const j = browser.tabs.query.callCount;
browser.tabs.sendMessage.resolves({});
browser.tabs.query.withArgs({
windowId: browser.windows.WINDOW_ID_CURRENT,
active: true,
windowType: 'normal'
}).resolves([{
id: 2,
windowId: 1
}]);
const res = await func(EDIT_HTML, {
id: 2
});
assert.strictEqual(browser.tabs.sendMessage.callCount, i + 1, 'called');
assert.strictEqual(browser.tabs.query.callCount, j, 'not called');
assert.deepEqual(res, {}, 'result');
});

it('should call function', async () => {
const i = browser.tabs.sendMessage.withArgs(2, {
info: {
menuItemId: MODE_EDIT_MD
},
tab: {
id: 2
}
}).callCount;
const j = browser.tabs.query.callCount;
browser.tabs.sendMessage.resolves({});
browser.tabs.query.withArgs({
windowId: browser.windows.WINDOW_ID_CURRENT,
active: true,
windowType: 'normal'
}).resolves([{
id: 2,
windowId: 1
}]);
const res = await func(EDIT_MD, {
id: 2
});
assert.strictEqual(browser.tabs.sendMessage.callCount, i + 1, 'called');
assert.strictEqual(browser.tabs.query.callCount, j, 'not called');
assert.deepEqual(res, {}, 'result');
});

it('should call function', async () => {
const i = browser.tabs.sendMessage.withArgs(2, {
info: {
menuItemId: MODE_EDIT_TXT
},
tab: {
id: 2
}
}).callCount;
const j = browser.tabs.query.callCount;
browser.tabs.sendMessage.resolves({});
browser.tabs.query.withArgs({
windowId: browser.windows.WINDOW_ID_CURRENT,
active: true,
windowType: 'normal'
}).resolves([{
id: 2,
windowId: 1
}]);
const res = await func(EDIT_TXT, {
id: 2
});
assert.strictEqual(browser.tabs.sendMessage.callCount, i + 1, 'called');
assert.strictEqual(browser.tabs.query.callCount, j, 'not called');
assert.deepEqual(res, {}, 'result');
});
});

describe('send variable', () => {
Expand Down

0 comments on commit c3ac2b4

Please sign in to comment.