2020-05-02 17:41:07 +02:00
|
|
|
import { useEffect } from 'react';
|
2020-10-09 19:35:46 +02:00
|
|
|
import { FormNote, ScrollOptionTypes } from './types';
|
2020-07-03 23:32:39 +02:00
|
|
|
import editorCommandDeclarations from '../commands/editorCommandDeclarations';
|
2020-11-05 18:58:23 +02:00
|
|
|
import CommandService, { CommandDeclaration, CommandRuntime, CommandContext } from '@joplinapp/lib/services/CommandService';
|
|
|
|
const time = require('@joplinapp/lib/time').default;
|
|
|
|
const { reg } = require('@joplinapp/lib/registry.js');
|
2020-07-03 23:32:39 +02:00
|
|
|
|
|
|
|
const commandsWithDependencies = [
|
|
|
|
require('../commands/showLocalSearch'),
|
|
|
|
require('../commands/focusElementNoteTitle'),
|
|
|
|
require('../commands/focusElementNoteBody'),
|
|
|
|
];
|
2020-05-02 17:41:07 +02:00
|
|
|
|
|
|
|
interface HookDependencies {
|
|
|
|
formNote:FormNote,
|
|
|
|
setShowLocalSearch:Function,
|
|
|
|
dispatch:Function,
|
|
|
|
noteSearchBarRef:any,
|
|
|
|
editorRef:any,
|
|
|
|
titleInputRef:any,
|
|
|
|
saveNoteAndWait: Function,
|
|
|
|
}
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
function editorCommandRuntime(declaration:CommandDeclaration, editorRef:any):CommandRuntime {
|
|
|
|
return {
|
2020-10-18 22:52:10 +02:00
|
|
|
execute: async (_context:CommandContext, ...args:any[]) => {
|
2020-07-03 23:32:39 +02:00
|
|
|
if (!editorRef.current.execCommand) {
|
|
|
|
reg.logger().warn('Received command, but editor cannot execute commands', declaration.name);
|
2020-10-18 22:52:10 +02:00
|
|
|
return;
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-10-18 22:52:10 +02:00
|
|
|
if (declaration.name === 'insertDateTime') {
|
|
|
|
return editorRef.current.execCommand({
|
|
|
|
name: 'insertText',
|
|
|
|
value: time.formatMsToLocal(new Date().getTime()),
|
|
|
|
});
|
|
|
|
} else if (declaration.name === 'scrollToHash') {
|
|
|
|
return editorRef.current.scrollTo({
|
|
|
|
type: ScrollOptionTypes.Hash,
|
|
|
|
value: args[0],
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return editorRef.current.execCommand({
|
|
|
|
name: declaration.name,
|
|
|
|
value: args[0],
|
|
|
|
});
|
|
|
|
}
|
2020-07-03 23:32:39 +02:00
|
|
|
},
|
2020-10-18 22:52:10 +02:00
|
|
|
enabledCondition: '!modalDialogVisible && markdownEditorPaneVisible && oneNoteSelected && noteIsMarkdown',
|
2020-07-03 23:32:39 +02:00
|
|
|
};
|
|
|
|
}
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
export default function useWindowCommandHandler(dependencies:HookDependencies) {
|
|
|
|
const { setShowLocalSearch, noteSearchBarRef, editorRef, titleInputRef } = dependencies;
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
useEffect(() => {
|
|
|
|
for (const declaration of editorCommandDeclarations) {
|
|
|
|
CommandService.instance().registerRuntime(declaration.name, editorCommandRuntime(declaration, editorRef));
|
|
|
|
}
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
const dependencies = {
|
|
|
|
editorRef,
|
|
|
|
setShowLocalSearch,
|
|
|
|
noteSearchBarRef,
|
|
|
|
titleInputRef,
|
|
|
|
};
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
for (const command of commandsWithDependencies) {
|
|
|
|
CommandService.instance().registerRuntime(command.declaration.name, command.runtime(dependencies));
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
return () => {
|
|
|
|
for (const declaration of editorCommandDeclarations) {
|
|
|
|
CommandService.instance().unregisterRuntime(declaration.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const command of commandsWithDependencies) {
|
|
|
|
CommandService.instance().unregisterRuntime(command.declaration.name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [editorRef, setShowLocalSearch, noteSearchBarRef, titleInputRef]);
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|