You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-11-23 22:36:32 +02:00
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { CommandContext, CommandDeclaration, CommandRuntime } from '../services/CommandService';
|
||||
import CommandService, { CommandContext, CommandDeclaration, CommandRuntime } from '../services/CommandService';
|
||||
import { _ } from '../locale';
|
||||
import Setting from '../models/Setting';
|
||||
import getActivePluginEditorView from '../services/plugins/utils/getActivePluginEditorView';
|
||||
import Logger from '@joplin/utils/Logger';
|
||||
import getActivePluginEditorViews from '../services/plugins/utils/getActivePluginEditorViews';
|
||||
import getShownPluginEditorView from '../services/plugins/utils/getShownPluginEditorView';
|
||||
|
||||
const logger = Logger.create('toggleEditorPlugin');
|
||||
|
||||
@@ -15,29 +15,34 @@ export const declaration: CommandDeclaration = {
|
||||
export const runtime = (): CommandRuntime => {
|
||||
return {
|
||||
execute: async (context: CommandContext) => {
|
||||
const shownEditorViewIds = Setting.value('plugins.shownEditorViewIds');
|
||||
const { editorPlugin, editorView } = getActivePluginEditorView(context.state.pluginService.plugins);
|
||||
const activeWindowId = context.state.windowId;
|
||||
const activePluginStates = getActivePluginEditorViews(context.state.pluginService.plugins, activeWindowId);
|
||||
|
||||
if (!editorPlugin) {
|
||||
if (activePluginStates.length === 0) {
|
||||
logger.warn('No editor plugin to toggle to');
|
||||
return;
|
||||
}
|
||||
|
||||
const idx = shownEditorViewIds.indexOf(editorView.id);
|
||||
let hasBeenHidden = false;
|
||||
let showedView = false;
|
||||
const setEditorPluginVisible = async (viewId: string, visible: boolean) => {
|
||||
await CommandService.instance().execute('showEditorPlugin', viewId, visible);
|
||||
showedView ||= visible;
|
||||
};
|
||||
|
||||
if (idx < 0) {
|
||||
shownEditorViewIds.push(editorView.id);
|
||||
} else {
|
||||
shownEditorViewIds.splice(idx, 1);
|
||||
hasBeenHidden = true;
|
||||
const { editorView: visibleView } = getShownPluginEditorView(context.state.pluginService.plugins, activeWindowId);
|
||||
// Hide the visible view
|
||||
if (visibleView) {
|
||||
await setEditorPluginVisible(visibleView.id, false);
|
||||
}
|
||||
|
||||
logger.info('New shown editor views: ', shownEditorViewIds);
|
||||
// Show the next view
|
||||
const visibleViewIndex = activePluginStates.findIndex(state => state.editorView.id === visibleView?.id);
|
||||
const nextIndex = visibleViewIndex + 1;
|
||||
if (nextIndex < activePluginStates.length) {
|
||||
await setEditorPluginVisible(activePluginStates[nextIndex].editorView.id, true);
|
||||
}
|
||||
|
||||
Setting.setValue('plugins.shownEditorViewIds', shownEditorViewIds);
|
||||
|
||||
if (hasBeenHidden) {
|
||||
if (!showedView) {
|
||||
// When the plugin editor goes from visible to hidden, we need to reload the note
|
||||
// because it may have been changed via the data API.
|
||||
context.dispatch({
|
||||
|
||||
Reference in New Issue
Block a user