From f85db1496edb098a16c67504f21b6f7e29bc29b1 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Mon, 25 Mar 2024 12:50:33 +0000 Subject: [PATCH] Desktop: Fixed error when exporting certain notes that contain plugin content --- .eslintignore | 3 +++ .gitignore | 3 +++ .../app-desktop/gui/NoteEditor/NoteEditor.tsx | 8 ++---- packages/app-desktop/testPluginDemo.sh | 2 +- .../interop/InteropService_Exporter_Html.ts | 2 ++ .../services/plugins/api/JoplinSettings.ts | 27 +++++++------------ .../utils/getPluginNamespacedSettingKey.ts | 9 +++++++ .../utils/getPluginSettingKeyPrefix.ts | 3 +++ .../plugins/utils/getPluginSettingValue.ts | 6 +++++ 9 files changed, 38 insertions(+), 25 deletions(-) create mode 100644 packages/lib/services/plugins/utils/getPluginNamespacedSettingKey.ts create mode 100644 packages/lib/services/plugins/utils/getPluginSettingKeyPrefix.ts create mode 100644 packages/lib/services/plugins/utils/getPluginSettingValue.ts diff --git a/.eslintignore b/.eslintignore index 13447f79a..af5cc2f29 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1022,6 +1022,9 @@ packages/lib/services/plugins/defaultPlugins/desktopDefaultPluginsInfo.js packages/lib/services/plugins/reducer.js packages/lib/services/plugins/utils/createViewHandle.js packages/lib/services/plugins/utils/executeSandboxCall.js +packages/lib/services/plugins/utils/getPluginNamespacedSettingKey.js +packages/lib/services/plugins/utils/getPluginSettingKeyPrefix.js +packages/lib/services/plugins/utils/getPluginSettingValue.js packages/lib/services/plugins/utils/loadContentScripts.js packages/lib/services/plugins/utils/makeListener.js packages/lib/services/plugins/utils/manifestFromObject.js diff --git a/.gitignore b/.gitignore index 5a054a346..e97e9bd5e 100644 --- a/.gitignore +++ b/.gitignore @@ -1002,6 +1002,9 @@ packages/lib/services/plugins/defaultPlugins/desktopDefaultPluginsInfo.js packages/lib/services/plugins/reducer.js packages/lib/services/plugins/utils/createViewHandle.js packages/lib/services/plugins/utils/executeSandboxCall.js +packages/lib/services/plugins/utils/getPluginNamespacedSettingKey.js +packages/lib/services/plugins/utils/getPluginSettingKeyPrefix.js +packages/lib/services/plugins/utils/getPluginSettingValue.js packages/lib/services/plugins/utils/loadContentScripts.js packages/lib/services/plugins/utils/makeListener.js packages/lib/services/plugins/utils/manifestFromObject.js diff --git a/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx b/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx index 3176a01cf..e33232d2c 100644 --- a/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx +++ b/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx @@ -49,7 +49,7 @@ import PlainEditor from './NoteBody/PlainEditor/PlainEditor'; import CodeMirror6 from './NoteBody/CodeMirror/v6/CodeMirror'; import CodeMirror5 from './NoteBody/CodeMirror/v5/CodeMirror'; import { openItemById } from './utils/contextMenu'; -import { namespacedKey } from '@joplin/lib/services/plugins/api/JoplinSettings'; +import getPluginSettingValue from '@joplin/lib/services/plugins/utils/getPluginSettingValue'; import { MarkupLanguage } from '@joplin/renderer'; const commands = [ @@ -162,10 +162,6 @@ function NoteEditor(props: NoteEditorProps) { return formNote.saveActionQueue.waitForAllDone(); } - const settingValue = useCallback((pluginId: string, key: string) => { - return Setting.value(namespacedKey(pluginId, key)); - }, []); - const whiteBackgroundNoteRendering = formNote.markup_language === MarkupLanguage.Html; const markupToHtml = useMarkupToHtml({ @@ -173,7 +169,7 @@ function NoteEditor(props: NoteEditorProps) { whiteBackgroundNoteRendering, customCss: props.customCss, plugins: props.plugins, - settingValue, + settingValue: getPluginSettingValue, }); const allAssets = useCallback(async (markupLanguage: number, options: AllAssetsOptions = null): Promise => { diff --git a/packages/app-desktop/testPluginDemo.sh b/packages/app-desktop/testPluginDemo.sh index da9021dec..53312c0b8 100755 --- a/packages/app-desktop/testPluginDemo.sh +++ b/packages/app-desktop/testPluginDemo.sh @@ -6,7 +6,7 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" TEMP_PATH=~/src/plugin-tests NEED_COMPILING=1 -PLUGIN_PATH=~/src/joplin/packages/app-cli/tests/support/plugins/note_list_renderer +PLUGIN_PATH=~/src/plugin-abc if [[ $NEED_COMPILING == 1 ]]; then mkdir -p "$TEMP_PATH" diff --git a/packages/lib/services/interop/InteropService_Exporter_Html.ts b/packages/lib/services/interop/InteropService_Exporter_Html.ts index b782e64f2..a6929cb55 100644 --- a/packages/lib/services/interop/InteropService_Exporter_Html.ts +++ b/packages/lib/services/interop/InteropService_Exporter_Html.ts @@ -13,6 +13,7 @@ import htmlpack from '@joplin/htmlpack'; const { themeStyle } = require('../../theme'); const { escapeHtml } = require('../../string-utils.js'); import { assetsToHeaders } from '@joplin/renderer'; +import getPluginSettingValue from '../plugins/utils/getPluginSettingValue'; export default class InteropService_Exporter_Html extends InteropService_Exporter_Base { @@ -112,6 +113,7 @@ export default class InteropService_Exporter_Html extends InteropService_Exporte const result = await this.markupToHtml_.render(item.markup_language, bodyMd, this.style_, { resources: this.resources_, plainResourceRendering: true, + settingValue: getPluginSettingValue, }); const noteContent = []; if (item.title) noteContent.push(`
${escapeHtml(item.title)}
`); diff --git a/packages/lib/services/plugins/api/JoplinSettings.ts b/packages/lib/services/plugins/api/JoplinSettings.ts index 8749da01f..00a65d9b2 100644 --- a/packages/lib/services/plugins/api/JoplinSettings.ts +++ b/packages/lib/services/plugins/api/JoplinSettings.ts @@ -3,6 +3,8 @@ import eventManager, { EventName } from '../../../eventManager'; import Setting, { SettingItem as InternalSettingItem, SettingSectionSource } from '../../../models/Setting'; import Plugin from '../Plugin'; +import getPluginNamespacedSettingKey from '../utils/getPluginNamespacedSettingKey'; +import getPluginSettingKeyPrefix from '../utils/getPluginSettingKeyPrefix'; import { SettingItem, SettingSection } from './types'; // That's all the plugin as of 27/08/21 - any new plugin after that will not be @@ -70,17 +72,6 @@ export interface ChangeEvent { export type ChangeHandler = (event: ChangeEvent)=> void; -const keyPrefix = (pluginId: string): string => { - return `plugin-${pluginId}.`; -}; - -// Ensures that the plugin settings and sections are within their own namespace, -// to prevent them from overwriting other plugin settings or the default -// settings. -export const namespacedKey = (pluginId: string, key: string): string => { - return `${keyPrefix(pluginId)}${key}`; -}; - /** * This API allows registering new settings and setting sections, as well as getting and setting settings. Once a setting has been registered it will appear in the config screen and be editable by the user. * @@ -117,7 +108,7 @@ export default class JoplinSettings { if ('subType' in setting) internalSettingItem.subType = setting.subType; if ('isEnum' in setting) internalSettingItem.isEnum = setting.isEnum; - if ('section' in setting) internalSettingItem.section = namespacedKey(this.plugin_.id, setting.section); + if ('section' in setting) internalSettingItem.section = getPluginNamespacedSettingKey(this.plugin_.id, setting.section); if ('options' in setting) internalSettingItem.options = () => setting.options; if ('appTypes' in setting) internalSettingItem.appTypes = setting.appTypes; if ('secure' in setting) internalSettingItem.secure = setting.secure; @@ -127,7 +118,7 @@ export default class JoplinSettings { if ('step' in setting) internalSettingItem.step = setting.step; if ('storage' in setting) internalSettingItem.storage = setting.storage; - await Setting.registerSetting(namespacedKey(this.plugin_.id, key), internalSettingItem); + await Setting.registerSetting(getPluginNamespacedSettingKey(this.plugin_.id, key), internalSettingItem); } } @@ -151,21 +142,21 @@ export default class JoplinSettings { * Registers a new setting section. Like for registerSetting, it is dynamic and needs to be done every time the plugin starts. */ public async registerSection(name: string, section: SettingSection) { - return Setting.registerSection(namespacedKey(this.plugin_.id, name), SettingSectionSource.Plugin, section); + return Setting.registerSection(getPluginNamespacedSettingKey(this.plugin_.id, name), SettingSectionSource.Plugin, section); } /** * Gets a setting value (only applies to setting you registered from your plugin) */ public async value(key: string): Promise { - return Setting.value(namespacedKey(this.plugin_.id, key)); + return Setting.value(getPluginNamespacedSettingKey(this.plugin_.id, key)); } /** * Sets a setting value (only applies to setting you registered from your plugin) */ public async setValue(key: string, value: any) { - return Setting.setValue(namespacedKey(this.plugin_.id, key), value); + return Setting.setValue(getPluginNamespacedSettingKey(this.plugin_.id, key), value); } /** @@ -188,8 +179,8 @@ export default class JoplinSettings { // Filter out keys that are not related to this plugin eventManager.on(EventName.SettingsChange, (event: ChangeEvent) => { const keys = event.keys - .filter(k => k.indexOf(keyPrefix(this.plugin_.id)) === 0) - .map(k => k.substr(keyPrefix(this.plugin_.id).length)); + .filter(k => k.indexOf(getPluginSettingKeyPrefix(this.plugin_.id)) === 0) + .map(k => k.substr(getPluginSettingKeyPrefix(this.plugin_.id).length)); if (!keys.length) return; handler({ keys }); }); diff --git a/packages/lib/services/plugins/utils/getPluginNamespacedSettingKey.ts b/packages/lib/services/plugins/utils/getPluginNamespacedSettingKey.ts new file mode 100644 index 000000000..5262f9868 --- /dev/null +++ b/packages/lib/services/plugins/utils/getPluginNamespacedSettingKey.ts @@ -0,0 +1,9 @@ +import getPluginSettingKeyPrefix from './getPluginSettingKeyPrefix'; + +// Ensures that the plugin settings and sections are within their own namespace, +// to prevent them from overwriting other plugin settings or the default +// settings. + +export default (pluginId: string, key: string): string => { + return `${getPluginSettingKeyPrefix(pluginId)}${key}`; +}; diff --git a/packages/lib/services/plugins/utils/getPluginSettingKeyPrefix.ts b/packages/lib/services/plugins/utils/getPluginSettingKeyPrefix.ts new file mode 100644 index 000000000..9d7b28849 --- /dev/null +++ b/packages/lib/services/plugins/utils/getPluginSettingKeyPrefix.ts @@ -0,0 +1,3 @@ +export default (pluginId: string): string => { + return `plugin-${pluginId}.`; +}; diff --git a/packages/lib/services/plugins/utils/getPluginSettingValue.ts b/packages/lib/services/plugins/utils/getPluginSettingValue.ts new file mode 100644 index 000000000..e0cf99512 --- /dev/null +++ b/packages/lib/services/plugins/utils/getPluginSettingValue.ts @@ -0,0 +1,6 @@ +import Setting from '../../../models/Setting'; +import getPluginNamespacedSettingKey from './getPluginNamespacedSettingKey'; + +export default (pluginId: string, key: string) => { + return Setting.value(getPluginNamespacedSettingKey(pluginId, key)); +};