* refactor: menuRow directive to MenuRow component * refactor: migrate footer to react * refactor: migrate actions menu to react * refactor: migrate history menu to react * fix: click outside handler use capture to trigger event before re-render occurs which would otherwise cause node.contains to return incorrect result (specifically for the account menu) * refactor: migrate revision preview modal to react * refactor: migrate permissions modal to react * refactor: migrate password wizard to react * refactor: remove unused input modal directive * refactor: remove unused delay hide component * refactor: remove unused filechange directive * refactor: remove unused elemReady directive * refactor: remove unused sn-enter directive * refactor: remove unused lowercase directive * refactor: remove unused autofocus directive * refactor(wip): note view to react * refactor: use mutation observer to deinit textarea listeners * refactor: migrate challenge modal to react * refactor: migrate note group view to react * refactor(wip): migrate remaining classes * fix: navigation parent ref * refactor: fully remove angular assets * fix: account switcher * fix: application view state * refactor: remove unused password wizard type * fix: revision preview and permissions modal * fix: remove angular comment * refactor: react panel resizers for editor * feat: simple panel resizer * fix: use simple panel resizer everywhere * fix: simplify panel resizer state * chore: rename simple panel resizer to panel resizer * refactor: simplify column layout * fix: editor mount safety check * fix: use inline onLoad callback for iframe, as setting onload after it loads will never call it * chore: fix note view test * chore(deps): upgrade snjs
180 lines
5.4 KiB
TypeScript
180 lines
5.4 KiB
TypeScript
import { Dropdown, DropdownItem } from '@/components/Dropdown';
|
|
import { IconType } from '@/components/Icon';
|
|
import { FeatureIdentifier, PrefKey } from '@standardnotes/snjs';
|
|
import {
|
|
PreferencesGroup,
|
|
PreferencesSegment,
|
|
Subtitle,
|
|
Text,
|
|
Title,
|
|
} from '@/preferences/components';
|
|
import { WebApplication } from '@/ui_models/application';
|
|
import {
|
|
ComponentArea,
|
|
ComponentMutator,
|
|
SNComponent,
|
|
} from '@standardnotes/snjs';
|
|
import { FunctionComponent } from 'preact';
|
|
import { useEffect, useState } from 'preact/hooks';
|
|
import { HorizontalSeparator } from '@/components/shared/HorizontalSeparator';
|
|
import { Switch } from '@/components/Switch';
|
|
|
|
type Props = {
|
|
application: WebApplication;
|
|
};
|
|
|
|
type EditorOption = DropdownItem & {
|
|
value: FeatureIdentifier | 'plain-editor';
|
|
};
|
|
|
|
export const getIconAndTintForEditor = (
|
|
identifier: FeatureIdentifier | undefined
|
|
): [IconType, number] => {
|
|
switch (identifier) {
|
|
case FeatureIdentifier.BoldEditor:
|
|
case FeatureIdentifier.PlusEditor:
|
|
return ['rich-text', 1];
|
|
case FeatureIdentifier.MarkdownBasicEditor:
|
|
case FeatureIdentifier.MarkdownMathEditor:
|
|
case FeatureIdentifier.MarkdownMinimistEditor:
|
|
case FeatureIdentifier.MarkdownProEditor:
|
|
return ['markdown', 2];
|
|
case FeatureIdentifier.TokenVaultEditor:
|
|
return ['authenticator', 6];
|
|
case FeatureIdentifier.SheetsEditor:
|
|
return ['spreadsheets', 5];
|
|
case FeatureIdentifier.TaskEditor:
|
|
return ['tasks', 3];
|
|
case FeatureIdentifier.CodeEditor:
|
|
return ['code', 4];
|
|
default:
|
|
return ['plain-text', 1];
|
|
}
|
|
};
|
|
|
|
const makeEditorDefault = (
|
|
application: WebApplication,
|
|
component: SNComponent,
|
|
currentDefault: SNComponent
|
|
) => {
|
|
if (currentDefault) {
|
|
removeEditorDefault(application, currentDefault);
|
|
}
|
|
application.changeAndSaveItem(component.uuid, (m) => {
|
|
const mutator = m as ComponentMutator;
|
|
mutator.defaultEditor = true;
|
|
});
|
|
};
|
|
|
|
const removeEditorDefault = (
|
|
application: WebApplication,
|
|
component: SNComponent
|
|
) => {
|
|
application.changeAndSaveItem(component.uuid, (m) => {
|
|
const mutator = m as ComponentMutator;
|
|
mutator.defaultEditor = false;
|
|
});
|
|
};
|
|
|
|
const getDefaultEditor = (application: WebApplication) => {
|
|
return application.componentManager
|
|
.componentsForArea(ComponentArea.Editor)
|
|
.filter((e) => e.isDefaultEditor())[0];
|
|
};
|
|
|
|
export const Defaults: FunctionComponent<Props> = ({ application }) => {
|
|
const [editorItems, setEditorItems] = useState<DropdownItem[]>([]);
|
|
const [defaultEditorValue, setDefaultEditorValue] = useState(
|
|
() =>
|
|
getDefaultEditor(application)?.package_info?.identifier || 'plain-editor'
|
|
);
|
|
|
|
const [spellcheck, setSpellcheck] = useState(() =>
|
|
application.getPreference(PrefKey.EditorSpellcheck, true)
|
|
);
|
|
|
|
const toggleSpellcheck = () => {
|
|
setSpellcheck(!spellcheck);
|
|
application.getAppState().toggleGlobalSpellcheck();
|
|
};
|
|
|
|
useEffect(() => {
|
|
const editors = application.componentManager
|
|
.componentsForArea(ComponentArea.Editor)
|
|
.map((editor): EditorOption => {
|
|
const identifier = editor.package_info.identifier;
|
|
const [iconType, tint] = getIconAndTintForEditor(identifier);
|
|
|
|
return {
|
|
label: editor.name,
|
|
value: identifier,
|
|
...(iconType ? { icon: iconType } : null),
|
|
...(tint ? { iconClassName: `color-accessory-tint-${tint}` } : null),
|
|
};
|
|
})
|
|
.concat([
|
|
{
|
|
icon: 'plain-text',
|
|
iconClassName: `color-accessory-tint-1`,
|
|
label: 'Plain Editor',
|
|
value: 'plain-editor',
|
|
},
|
|
])
|
|
.sort((a, b) => {
|
|
return a.label.toLowerCase() < b.label.toLowerCase() ? -1 : 1;
|
|
});
|
|
|
|
setEditorItems(editors);
|
|
}, [application]);
|
|
|
|
const setDefaultEditor = (value: string) => {
|
|
setDefaultEditorValue(value as FeatureIdentifier);
|
|
const editors = application.componentManager.componentsForArea(
|
|
ComponentArea.Editor
|
|
);
|
|
const currentDefault = getDefaultEditor(application);
|
|
|
|
if (value !== 'plain-editor') {
|
|
const editorComponent = editors.filter(
|
|
(e) => e.package_info.identifier === value
|
|
)[0];
|
|
makeEditorDefault(application, editorComponent, currentDefault);
|
|
} else {
|
|
removeEditorDefault(application, currentDefault);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<PreferencesGroup>
|
|
<PreferencesSegment>
|
|
<Title>Defaults</Title>
|
|
<div>
|
|
<Subtitle>Default Editor</Subtitle>
|
|
<Text>New notes will be created using this editor.</Text>
|
|
<div className="mt-2">
|
|
<Dropdown
|
|
id="def-editor-dropdown"
|
|
label="Select the default editor"
|
|
items={editorItems}
|
|
value={defaultEditorValue}
|
|
onChange={setDefaultEditor}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<HorizontalSeparator classes="mt-5 mb-3" />
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex flex-col">
|
|
<Subtitle>Spellcheck</Subtitle>
|
|
<Text>
|
|
The default spellcheck value for new notes. Spellcheck can be
|
|
configured per note from the note context menu. Spellcheck may
|
|
degrade overall typing performance with long notes.
|
|
</Text>
|
|
</div>
|
|
<Switch onChange={toggleSpellcheck} checked={spellcheck} />
|
|
</div>
|
|
</PreferencesSegment>
|
|
</PreferencesGroup>
|
|
);
|
|
};
|