Skip to content

Commit

Permalink
Fixed #506. Clarified behavior of localized setting.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Jul 6, 2024
1 parent 4e11764 commit 9d2fedf
Show file tree
Hide file tree
Showing 26 changed files with 172 additions and 89 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Dates are in `YYYY-MM-DD` format and versions are in [semantic versioning](http:
- [#507](https://github.com/wordplaydev/wordplay/issues/507) Fixed Webpage stream replay bug.
- [#216](https://github.com/wordplaydev/wordplay/issues/216) Improved design of view code and copy buttons.
- [#397](https://github.com/wordplaydev/wordplay/issues/397) Redesigned home page for clarity and navigability.
- [#506](https://github.com/wordplaydev/wordplay/issues/506) Clarified behavior of localized setting.
- Added fade out sequence.
- Fixed select all button.

Expand Down
4 changes: 2 additions & 2 deletions src/components/concepts/BindConceptView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
>{#if concept.bind.type}•<RootView
node={concept.bind.type}
inline
localized
localized="symbolic"
/>{#if concept.bind.value}: <RootView
node={concept.bind.value}
inline
localized
localized="symbolic"
/>{/if}{/if}
</svelte:fragment>
</Speech>
2 changes: 1 addition & 1 deletion src/components/concepts/ConceptView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
{/if}{@const name = variable.names.getPreferredName(
$locales.getLocales(),
)}{#if name}<RootView
localized
localized="symbolic"
node={name.withoutLanguage()}
/>{/if}{/each}{/if}</svelte:fragment
>
Expand Down
2 changes: 1 addition & 1 deletion src/components/concepts/SegmentHTMLView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
{:else}<RootView
node={segment.node}
inline
localized
localized="symbolic"
/>{/if}{:else if segment instanceof ValueRef}<strong
><ValueView value={segment.value} /></strong
>{:else if segment instanceof ConceptRef}<ConceptLinkUI link={segment} />
Expand Down
2 changes: 1 addition & 1 deletion src/components/concepts/TypeView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
concept && (event.key === 'Enter' || event.key === ' ')
? navigate(concept)
: undefined}
><RootView node={type} inert inline localized /></span
><RootView node={type} inert inline localized="symbolic" /></span
>{/each}
</span>

Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/EvaluateView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
node={nextBind.withoutValue()}
inline
elide
localized
localized="symbolic"
inert
/>{#if menuPosition}<PlaceholderView
position={menuPosition}
Expand Down
9 changes: 7 additions & 2 deletions src/components/editor/ExpressionPlaceholderView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
node,
$locales,
context,
$root
$root,
);
} else placeholder = undefined;
}
Expand All @@ -48,7 +48,12 @@
/>{:else if inferredType && !(inferredType instanceof UnknownType)}<span
>•</span
><div class:inferred={node.type === undefined && inferredType}
><RootView elide inert localized node={inferredType} /></div
><RootView
elide
inert
localized="symbolic"
node={inferredType}
/></div
>{/if}{#if caret}<PlaceholderView position={node} />{/if}</span
></span
>
Expand Down
9 changes: 6 additions & 3 deletions src/components/editor/Menu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,13 @@
{#if newNode !== undefined}
{#if revision.isRemoval()}
<strike
><RootView node={newNode} localized /></strike
><RootView
node={newNode}
localized="symbolic"
/></strike
>
{:else}
<RootView node={newNode} localized />
<RootView node={newNode} localized="symbolic" />
{/if}
{:else}
<MarkupHTMLView
Expand Down Expand Up @@ -232,7 +235,7 @@
newNode.statements.length > 1
? newNode
: newNode}
localized
localized="symbolic"
/>
</div>
{/if}
Expand Down
26 changes: 21 additions & 5 deletions src/components/editor/TokenView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,39 @@
? node.getPlaceholder($root, context, $locales)
: undefined;
$: isInCaret =
$caret &&
node.getTextLength() > 0 &&
($caret.getTokenExcludingSpace() === node ||
($caret.tokenPrior === node && $caret.atBeginningOfTokenSpace()));
// True if the caret is "on" this token.
$: active =
$caret &&
node.getTextLength() > 0 &&
($caret?.getTokenExcludingSpace() === node ||
($caret?.tokenPrior === node &&
($caret.getTokenExcludingSpace() === node ||
($caret.tokenPrior === node &&
$caret.atBeginningOfTokenSpace() &&
$caret.tokenIncludingSpace &&
$caret.tokenAtHasPrecedingSpace()));
// True if this is the recently added token.
$: added = $caret?.addition?.contains(node) ?? false;
// Localize the token's text using the preferred translation.
// If requesed, localize the token's text.
// Don't localize the name if the caret is in the name.
$: text =
context && $root && localize && $localize
? node.localized($locales.getLocales(), $root, context)
!isInCaret &&
context &&
$root &&
localize &&
($localize === 'localized' || $localize === 'symbolic')
? node.localized(
$localize === 'symbolic',
$locales.getLocales(),
$root,
context,
)
: node.getText();
// Prepare the text for rendering by replacing spaces with non-breaking spaces
Expand Down
16 changes: 8 additions & 8 deletions src/components/palette/ContentEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
value instanceof Evaluate &&
(value.is(
project.shares.output.Phrase,
project.getNodeContext(value)
project.getNodeContext(value),
) ||
value.is(
project.shares.output.Group,
project.getNodeContext(value)
project.getNodeContext(value),
) ||
value.is(
project.shares.output.Shape,
project.getNodeContext(value)
))
project.getNodeContext(value),
)),
);
function editContent(index: number) {
Expand Down Expand Up @@ -82,7 +82,7 @@
active={editable}
action={() => editContent(index)}>{EDIT_SYMBOL}</Button
>
<RootView node={content} localized />
<RootView node={content} localized="symbolic" />
</div>
{/each}
<div class="add">
Expand All @@ -96,7 +96,7 @@
project,
list,
list?.values.length ?? 1 - 1,
'phrase'
'phrase',
)
: undefined}
>+{project.shares.output.Phrase.getNames()[0]}</Button
Expand All @@ -111,7 +111,7 @@
project,
list,
list?.values.length ?? 1 - 1,
'group'
'group',
)
: undefined}
>+{project.shares.output.Group.getNames()[0]}</Button
Expand All @@ -126,7 +126,7 @@
project,
list,
list?.values.length ?? 1 - 1,
'shape'
'shape',
)
: undefined}
>+{project.shares.output.Shape.getNames()[0]}</Button
Expand Down
3 changes: 2 additions & 1 deletion src/components/project/Contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
import type { CaretPosition } from '../../edit/Caret';
import type LanguageCode from '../../locale/LanguageCode';
import type Spaces from '@parser/Spaces';
import type { LocalizedValue } from '@db/LocalizedSetting';

// App related contexts

Expand Down Expand Up @@ -186,7 +187,7 @@ export function getHidden() {

export const LocalizeSymbol = Symbol('localize');
export function getLocalize() {
return getContext<Writable<boolean> | undefined>(LocalizeSymbol);
return getContext<Writable<LocalizedValue> | undefined>(LocalizeSymbol);
}

export const ConceptPathSymbol = Symbol('palette-path');
Expand Down
42 changes: 28 additions & 14 deletions src/components/project/ProjectView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
import { AnimationFactorIcons } from '@db/AnimationFactorSetting';
import { COPY_SYMBOL } from '@parser/Symbols';
import CopyButton from './CopyButton.svelte';
import { toLocaleString } from '@locale/Locale';
import { default as ModeChooser } from '@components/widgets/Mode.svelte';
export let project: Project;
export let original: Project | undefined = undefined;
Expand Down Expand Up @@ -1404,21 +1406,33 @@
toggle={toggleBlocks}
on={$blocks}
/>
<Switch
onLabel={$locales.getLocale().language}
onTip={$locales.get(
<ModeChooser
labeled={false}
descriptions={$locales.get(
(l) =>
l.ui.source.toggle.localized.on,
)}
offLabel={withVariationSelector('🌎')}
offTip={$locales.get(
(l) =>
l.ui.source.toggle.localized
.off,
l.ui.dialog.settings.mode
.localized,
)}
toggle={(on) =>
Settings.setLocalized(on)}
on={$localized}
choice={$localized === 'actual'
? 0
: $localized === 'localized'
? 1
: 2}
select={(choice) =>
Settings.setLocalized(
choice === 0
? 'actual'
: choice === 1
? 'localized'
: 'symbolic',
)}
modes={[
'...',
toLocaleString(
$locales.getLocale(),
),
'😀',
]}
/>
<!-- Make a Button for every navigate command -->
{#each VisibleNavigateCommands as command}<CommandButton
Expand Down Expand Up @@ -1648,7 +1662,7 @@
<RootView
node={$dragged}
spaces={project.getSourceOf($dragged)?.spaces}
localized
localized={$localized}
/>
<div class="cursor">🐲</div>
</div>
Expand Down
7 changes: 4 additions & 3 deletions src/components/project/RootView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import FormattedLiteral from '../../nodes/FormattedLiteral';
import type Caret from '@edit/Caret';
import getPreferredSpaces from '@parser/getPreferredSpaces';
import type { LocalizedValue } from '@db/LocalizedSetting';
export let node: Node;
/** Optional space. To enable preferred space, set flag below. */
Expand All @@ -34,7 +35,7 @@
/** If inline, and true, this will be a maximum width */
export let elide = false;
/** If true, hides names and docs not in a selected locale */
export let localized = false;
export let localized: LocalizedValue = 'symbolic';
export let caret: Caret | undefined = undefined;
/** Get the root, or make one if it's not a source. */
Expand All @@ -58,9 +59,9 @@
let hidden = writable<Set<Node>>(new Set());
setContext(HiddenSymbol, hidden);
let localize = writable<boolean>(localized);
let localize = writable<LocalizedValue>(localized ?? 'symbolic');
setContext(LocalizeSymbol, localize);
$: localize.set(localized);
$: localize.set(localized ?? 'symbolic');
// Update what's hidden.
$: {
Expand Down
5 changes: 4 additions & 1 deletion src/components/widgets/Mode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
export let choice: number;
export let select: (choice: number) => void;
export let active = true;
export let labeled = true;
</script>

<div class="mode">
<span class="label" id={descriptions.label}>{descriptions.label}</span>
{#if labeled}
<span class="label" id={descriptions.label}>{descriptions.label}</span>
{/if}
<div class="group" role="radiogroup" aria-labelledby={descriptions.label}>
{#each modes as mode, index}
<!-- We prevent mouse down default to avoid stealing keyboard focus. -->
Expand Down
23 changes: 19 additions & 4 deletions src/db/LocalizedSetting.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import Setting from './Setting';

export const LocalizedSetting = new Setting<boolean>(
export type LocalizedValue = 'actual' | 'localized' | 'symbolic';

export const LocalizedSetting = new Setting<LocalizedValue>(
'localized',
true,
false,
() => true,
(current, value) => current === value
'localized',
(value) =>
// An old boolean value? Convert to one of the three states.
typeof value === 'boolean'
? value
? 'localized'
: 'actual'
: // A string value? Check if it's one of the three states, and default to localized if not.
typeof value === 'string' &&
(value === 'actual' ||
value === 'localized' ||
value === 'symbolic')
? undefined
: // Some other mystery type? Default to localized.
'localized',
(current, value) => current === value,
);
5 changes: 3 additions & 2 deletions src/db/Setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class Setting<Type> {
device: boolean,
defaultValue: Type,
validator: (value: unknown) => Type | undefined,
equal: (current: Type, value: Type) => boolean
equal: (current: Type, value: Type) => boolean,
) {
this.key = key;
this.device = device;
Expand All @@ -39,7 +39,8 @@ export default class Setting<Type> {
try {
if (valueString !== null) {
const parsed = JSON.parse(valueString);
if (this.validator(parsed)) value = parsed;
const validation = this.validator(parsed);
value = validation;
}
if (value === undefined) value = this.defaultValue;
} catch (_) {
Expand Down
Loading

0 comments on commit 9d2fedf

Please sign in to comment.