Skip to content

Commit

Permalink
feat(wizard-dialog): require pro mode for code editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ca-d committed Jul 1, 2021
1 parent 572d993 commit 8fdb5dd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
23 changes: 21 additions & 2 deletions src/Setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import { Switch } from '@material/mwc-switch';

import { ifImplemented, LitElementConstructor, Mixin } from './foundation.js';
import { Language, languages, loader } from './translations/loader.js';
import { WizardDialog } from './wizard-dialog.js';

export type Settings = {
language: Language;
theme: 'light' | 'dark';
mode: 'safe' | 'pro';
};
export const defaults: Settings = {
language: 'en',
theme: 'light',
mode: 'safe',
};
export const defaults: Settings = { language: 'en', theme: 'light' };

/** Mixin that saves [[`Settings`]] to `localStorage`, reflecting them in the
* `settings` property, setting them through `setSetting(setting, value)`. */
Expand All @@ -26,6 +32,7 @@ export function Setting<TBase extends LitElementConstructor>(Base: TBase) {
return {
language: this.getSetting('language'),
theme: this.getSetting('theme'),
mode: this.getSetting('mode'),
};
}

Expand All @@ -35,6 +42,8 @@ export function Setting<TBase extends LitElementConstructor>(Base: TBase) {
languageUI!: Select;
@query('#dark')
darkThemeUI!: Switch;
@query('#mode')
modeUI!: Switch;

private getSetting<T extends keyof Settings>(setting: T): Settings[T] {
return (
Expand All @@ -44,6 +53,9 @@ export function Setting<TBase extends LitElementConstructor>(Base: TBase) {
/** Update the `value` of `setting`, storing to `localStorage`. */
setSetting<T extends keyof Settings>(setting: T, value: Settings[T]): void {
localStorage.setItem(setting, <string>(<unknown>value));
this.shadowRoot
?.querySelector<WizardDialog>('wizard-dialog')
?.requestUpdate();
this.requestUpdate();
}

Expand All @@ -56,6 +68,7 @@ export function Setting<TBase extends LitElementConstructor>(Base: TBase) {
} else if (ae.detail?.action === 'save') {
this.setSetting('language', <Language>this.languageUI.value);
this.setSetting('theme', this.darkThemeUI.checked ? 'dark' : 'light');
this.setSetting('mode', this.modeUI.checked ? 'pro' : 'safe');
this.requestUpdate('settings');
}
}
Expand All @@ -81,7 +94,7 @@ export function Setting<TBase extends LitElementConstructor>(Base: TBase) {
>
<form>
<mwc-select
naturalMenuWidth
fixedMenuPosition
id="language"
icon="language"
label="${translate('settings.language')}"
Expand All @@ -102,6 +115,12 @@ export function Setting<TBase extends LitElementConstructor>(Base: TBase) {
?checked=${this.settings.theme === 'dark'}
></mwc-switch>
</mwc-formfield>
<mwc-formfield label="${translate('settings.mode')}">
<mwc-switch
id="mode"
?checked=${this.settings.mode === 'pro'}
></mwc-switch>
</mwc-formfield>
</form>
<mwc-button slot="secondaryAction" dialogAction="close">
${translate('cancel')}
Expand Down
2 changes: 1 addition & 1 deletion src/open-scd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class OpenSCD extends Hosting(
}
mwc-dialog {
--mdc-dialog-max-width: 92vw;
--mdc-dialog-max-width: 98vw;
}
mwc-dialog > form {
Expand Down
1 change: 1 addition & 0 deletions src/translations/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const de: Translations = {
language: 'Sprache',
languages: { de: 'Deutsch', en: 'Englisch (English)' },
dark: 'Dunkles Design',
mode: 'Profimodus',
},
menu: {
title: 'Menü',
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const en = {
language: 'Language',
languages: { de: 'German (Deutsch)', en: 'English' },
dark: 'Dark theme',
mode: 'Pro mode',
},
menu: {
title: 'Menu',
Expand Down
8 changes: 6 additions & 2 deletions src/wizard-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ export class WizardDialog extends LitElement {
}

get code(): boolean {
return this.dialog?.querySelector('mwc-icon-button-toggle')?.on ?? false;
return (
(this.dialog?.querySelector('mwc-icon-button-toggle')?.on ?? false) &&
localStorage.getItem('mode') === 'pro'
);
}

/** Checks the inputs of all [[`WizardPage`]]s for validity. */
Expand Down Expand Up @@ -183,7 +186,7 @@ export class WizardDialog extends LitElement {
heading=${page.title}
@closed=${this.onClosed}
>
${page.element
${page.element && localStorage.getItem('mode') === 'pro'
? html`<mwc-icon-button-toggle
onicon="code"
officon="code_off"
Expand Down Expand Up @@ -267,6 +270,7 @@ export class WizardDialog extends LitElement {
position: absolute;
top: 8px;
right: 14px;
color: var(--base00);
}
mwc-dialog > mwc-icon-button-toggle[on] {
Expand Down
3 changes: 3 additions & 0 deletions src/wizards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { repeat } from 'lit-html/directives/repeat';
import { get, translate } from 'lit-translate';

import {
Create,
Delete,
EditorAction,
identity,
isEqual,
Expand All @@ -13,6 +15,7 @@ import {
Wizard,
WizardActor,
} from './foundation.js';
import { WizardDialog } from './wizard-dialog.js';

interface MergeOptions {
title?: string;
Expand Down

0 comments on commit 8fdb5dd

Please sign in to comment.