Skip to content

Commit

Permalink
Explicit code localization switch.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Nov 12, 2023
1 parent 92a724a commit 999ebe6
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 43 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Dates are in `YYYY-MM-DD` format and versions are in [semantic versioning](http:

## 0.9.1

This version includes many other changes made prior to this log was created.

## Added

- New change password form on profile.
Expand All @@ -13,6 +15,7 @@ Dates are in `YYYY-MM-DD` format and versions are in [semantic versioning](http:

- Improved design of username and password login [#272](https://github.com/wordplaydev/wordplay/issues/272), [#273](https://github.com/wordplaydev/wordplay/issues/273)
- Converted blocks toggle to a keyboard/mouse switch.
- Explicit switch control over localization of editor, instead of automatic.

## Fixed

Expand Down
8 changes: 7 additions & 1 deletion src/components/editor/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import Adjust from './Adjust.svelte';
import EditorHelp from './EditorHelp.svelte';
import Emoji from '@components/app/Emoji.svelte';
import { localized } from '../../db/Database';
const SHOW_OUTPUT_IN_PALETTE = false;
Expand Down Expand Up @@ -1448,7 +1449,12 @@
on:focusout={() => (focused = false)}
/>
<!-- Render the program -->
<RootView node={program} spaces={source.spaces} localized />
<RootView
node={program}
spaces={source.spaces}
localized={$localized}
caret={$caret}
/>
<!-- Render highlights above the code -->
{#each outlines as outline}
<Highlight {...outline} types={outline.types} above={true} />
Expand Down
6 changes: 3 additions & 3 deletions src/components/editor/util/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type Evaluator from '@runtime/Evaluator';
import FunctionDefinition from '@nodes/FunctionDefinition';
import ExpressionPlaceholder from '@nodes/ExpressionPlaceholder';
import Names from '@nodes/Names';
import type { Database } from '@db/Database';
import { Settings, type Database } from '@db/Database';
import type Locale from '@locale/Locale';
import Sym from '../../../nodes/Sym';
import type Project from '../../../models/Project';
Expand Down Expand Up @@ -90,7 +90,7 @@ export type CommandContext = {
database: Database;
dragging: boolean;
toggleMenu?: () => void;
toggleBlocks?: () => void;
toggleBlocks?: (on: boolean) => void;
setFullscreen?: (on: boolean) => void;
focusOrCycleTile?: (content?: TileKind) => void;
resetInputs?: () => void;
Expand Down Expand Up @@ -575,7 +575,7 @@ export const ToggleBlocks: Command = {
key: 'Enter',
execute: ({ toggleBlocks }) => {
if (toggleBlocks) {
toggleBlocks();
toggleBlocks(!Settings.getBlocks());
return true;
}
return false;
Expand Down
20 changes: 18 additions & 2 deletions src/components/project/ProjectView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
Projects,
writingLayout,
blocks,
localized,
Creators,
} from '../../db/Database';
import Arrangement from '../../db/Arrangement';
Expand Down Expand Up @@ -781,8 +782,8 @@
};
});
function toggleBlocks() {
Settings.setBlocks(!$blocks);
function toggleBlocks(on: boolean) {
Settings.setBlocks(on);
}
function getTileView(tileID: string) {
Expand Down Expand Up @@ -1360,6 +1361,21 @@
toggle={toggleBlocks}
on={$blocks}
/>
<Switch
onLabel={$locales.getLocale().language}
onTip={$locales.get(
(l) =>
l.ui.source.toggle.localized.on
)}
offLabel={withVariationSelector('🌎')}
offTip={$locales.get(
(l) =>
l.ui.source.toggle.localized.off
)}
toggle={(on) =>
Settings.setLocalized(on)}
on={$localized}
/>
<!-- Make a Button for every modify command -->
{#each VisibleModifyCommands as command}<CommandButton
{command}
Expand Down
79 changes: 43 additions & 36 deletions src/components/project/RootView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
SpaceSymbol,
type SpaceContext,
CaretSymbol,
getEditor,
LocalizeSymbol,
} from './Contexts';
import Root from '@nodes/Root';
Expand All @@ -25,6 +24,7 @@
import { locales } from '../../db/Database';
import TextLiteral from '../../nodes/TextLiteral';
import FormattedLiteral from '../../nodes/FormattedLiteral';
import type Caret from '@edit/Caret';
export let node: Node;
/** Optional space; if not provided, all nodes are rendered with preferred space. */
Expand All @@ -35,6 +35,7 @@
export let elide = false;
/** If true, hides names and docs not in a selected locale */
export let localized = false;
export let caret: Caret | undefined = undefined;
/** Get the root, or make one if it's not a source. */
$: root = node instanceof Source ? node.root : new Root(node);
Expand Down Expand Up @@ -80,17 +81,15 @@
renderedSpace.set(newSpace);
}
let editor = getEditor();
// A set of hidden nodes, such as hidden translations.
let hidden = writable<Set<Node>>(new Set());
setContext(HiddenSymbol, hidden);
let localize = writable<boolean>(localized);
setContext(LocalizeSymbol, localize);
$: localize.set(localized && ($editor === undefined || !$editor.focused));
$: localize.set(localized);
// When the caret changes, the update what's hidden.
// Update what's hidden.
$: {
const newHidden = new Set<Node>();
Expand All @@ -106,42 +105,50 @@
n instanceof TextLiteral ||
n instanceof FormattedLiteral
)) {
// Get all the names or docs
// Get the language tags on the nodes.
const tags = tagged.getTags();
// If at least one is visible, hide all those not in a preferred language.
if (
$locales
.getLanguages()
.some((lang) =>
tags.some((l) => l.getLanguage() === lang)
)
) {
let first = false;
for (const nameOrDoc of tags) {
const selectedLocale = $locales
// Is this caret inside this node?
const inside =
caret === undefined ? false : caret.isIn(tagged, true);
// If the caret is not inside the node, decide whether to hide.
if (!inside) {
// If at least one is visible, hide all those not in a preferred language.
if (
$locales
.getLanguages()
.some((t) => t === nameOrDoc.getLanguage());
// Not a selected language and not in the node and has a language? Hide it.
if (!selectedLocale && nameOrDoc.language)
newHidden.add(nameOrDoc);
// Is the selected language and inert? Hide the language tag.
else if (selectedLocale && nameOrDoc.language)
newHidden.add(nameOrDoc.language);
// Not first? Hide the separator.
if (!first) {
first = true;
if (
nameOrDoc instanceof Name &&
nameOrDoc.separator
.some((lang) =>
tags.some((l) => l.getLanguage() === lang)
)
newHidden.add(nameOrDoc.separator);
) {
let first = false;
for (const nameOrDoc of tags) {
const selectedLocale = $locales
.getLanguages()
.some((t) => t === nameOrDoc.getLanguage());
// Not a selected language and not in the node and has a language? Hide it.
if (!selectedLocale && nameOrDoc.language)
newHidden.add(nameOrDoc);
// Is the selected language and inert? Hide the language tag.
else if (selectedLocale && nameOrDoc.language)
newHidden.add(nameOrDoc.language);
// Not first? Hide the separator.
if (!first) {
first = true;
if (
nameOrDoc instanceof Name &&
nameOrDoc.separator
)
newHidden.add(nameOrDoc.separator);
}
}
}
}
// Otherwise, hide all but the first.
else {
for (const nameOrDoc of tags.slice(1))
newHidden.add(nameOrDoc);
// Otherwise, hide all but the first name
else {
for (const nameOrDoc of tags.slice(1))
newHidden.add(nameOrDoc);
}
}
// If this is a doc and we're not in a program, hide it unconditionally.
Expand Down
1 change: 1 addition & 0 deletions src/db/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ export const camera = Settings.settings.camera.value;
export const dark = Settings.settings.dark.value;
export const mic = Settings.settings.mic.value;
export const blocks = Settings.settings.blocks.value;
export const localized = Settings.settings.localized.value;
export const status = DB.Status;
export const editableProjects = Projects.allEditableProjects;
export const archivedProjects = Projects.allArchivedProjects;
Expand Down
9 changes: 9 additions & 0 deletions src/db/LocalizedSetting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Setting from './Setting';

export const LocalizedSetting = new Setting<boolean>(
'localized',
true,
false,
() => true,
(current, value) => current === value
);
14 changes: 14 additions & 0 deletions src/db/SettingsDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { WritingLayout } from '../locale/Scripts';
import type Progress from '../tutorial/Progress';
import Layout from '../components/project/Layout';
import { BlocksSetting } from './BlocksSetting';
import { LocalizedSetting } from './LocalizedSetting';
import { DarkSetting } from './DarkSetting';
import { doc, getDoc } from 'firebase/firestore';
import { firestore } from './firebase';
Expand Down Expand Up @@ -60,6 +61,7 @@ export default class SettingsDatabase {
camera: CameraSetting,
mic: MicSetting,
blocks: BlocksSetting,
localized: LocalizedSetting,
dark: DarkSetting,
};

Expand Down Expand Up @@ -170,6 +172,18 @@ export default class SettingsDatabase {
this.settings.blocks.set(this.database, on);
}

getBlocks() {
return this.settings.blocks.get();
}

getLocalized() {
return this.settings.localized.get();
}

setLocalized(on: boolean) {
this.settings.localized.set(this.database, on);
}

/** To serialize to a database */
toObject(): SettingsSchema {
// Get the config, but delete all device-specific configs.
Expand Down
2 changes: 2 additions & 0 deletions src/locale/UITexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ type UITexts = {
blocks: ToggleText;
/** The glyph chooser expand/collapse toggle */
glyphs: ToggleText;
/** The localized on/off toggle */
localized: ToggleText;
};
button: {
/** Output preview button for selecting output for display in output tile */
Expand Down
4 changes: 4 additions & 0 deletions src/locale/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -3817,6 +3817,10 @@
"on": "hide block backgrounds",
"off": "show block backgrounds"
},
"localized": {
"on": "show localized text",
"off": "show all text"
},
"glyphs": {
"on": "collapse the matching glyphs",
"off": "expand the matching glyphs"
Expand Down
4 changes: 4 additions & 0 deletions static/locales/es-MX/es-MX.json
Original file line number Diff line number Diff line change
Expand Up @@ -3881,6 +3881,10 @@
"on": "esconder los fondos de los bloques",
"off": "mostrar los fondos de los bloques"
},
"localized": {
"on": "$?",
"off": "$?"
},
"glyphs": {
"on": "colapsar los glifos coincidentes",
"off": "expandir los glifos coincidentes"
Expand Down
4 changes: 4 additions & 0 deletions static/locales/example/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -2159,6 +2159,10 @@
"on": "$?",
"off": "$?"
},
"localized": {
"on": "$?",
"off": "$?"
},
"glyphs": {
"on": "$?",
"off": "$?"
Expand Down
4 changes: 4 additions & 0 deletions static/locales/zh-CN/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -3702,6 +3702,10 @@
"on": "隐藏块背景",
"off": "显示块背景"
},
"localized": {
"on": "$?",
"off": "$?"
},
"glyphs": {
"on": "折叠匹配的字形",
"off": "展开匹配的字形"
Expand Down
7 changes: 6 additions & 1 deletion static/schemas/Locale.json
Original file line number Diff line number Diff line change
Expand Up @@ -9932,11 +9932,16 @@
"glyphs": {
"$ref": "#/definitions/ToggleText",
"description": "The glyph chooser expand/collapse toggle"
},
"localized": {
"$ref": "#/definitions/ToggleText",
"description": "The localized on/off toggle"
}
},
"required": [
"blocks",
"glyphs"
"glyphs",
"localized"
],
"type": "object"
}
Expand Down

0 comments on commit 999ebe6

Please sign in to comment.