Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ export function buildStringAliasedNodeData(
details: [],
};
}

export function getOnlyFilledLanguageCode(
nodeValue: Record<string, LanguageValue> | null | undefined,
): string | undefined {
const filledLanguageCodes = Object.entries(nodeValue ?? {})
.filter(([, languageValue]) => languageValue.value !== "")
.map(([languageCode]) => languageCode);
if (filledLanguageCodes.length === 1) {
return filledLanguageCodes[0];
}
return undefined;
}
Comment thread
chrabyrd marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const { aliasedNodeData, value, mode } = defineProps<RichTextWidgetProps>();
const emit = defineEmits<{
"update:value": [updatedValue: Record<string, LanguageValue> | null];
"update:aliasedNodeData": [updatedValue: StringAliasedNodeData];
"update:selectedLanguage": [languageCode: string];
initialized: [updatedValue: StringAliasedNodeData];
ready: [];
}>();
Expand Down Expand Up @@ -50,6 +51,7 @@ function onUpdateAliasedNodeData(
:card-x-node-x-widget-data="cardXNodeXWidgetData"
:aliased-node-data="resolvedAliasedNodeData"
@update:aliased-node-data="onUpdateAliasedNodeData"
@update:selected-language="emit('update:selectedLanguage', $event)"
@ready="emit('ready')"
/>
<RichTextWidgetViewer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { ref, watchEffect } from "vue";
import { ref, watch, watchEffect } from "vue";

import { useGettext } from "vue3-gettext";
import Editor from "openvue/editor";
Expand All @@ -8,7 +8,10 @@ import Select from "openvue/select";
import FocusController from "@/arches_vue_components/widgets/RichTextWidget/components/RichTextWidgetEditor/components/FocusController.vue";

import { fetchLanguages } from "@/arches_vue_components/widgets/api.ts";
import { buildStringAliasedNodeData } from "@/arches_vue_components/datatypes/string/utils.ts";
import {
buildStringAliasedNodeData,
getOnlyFilledLanguageCode,
} from "@/arches_vue_components/datatypes/string/utils.ts";

import type {
StringCardXNodeXWidgetData,
Expand All @@ -24,11 +27,14 @@ const { cardXNodeXWidgetData = undefined, aliasedNodeData } = defineProps<{
aliasedNodeData: StringAliasedNodeData | null;
}>();

const initialNodeValue = aliasedNodeData?.node_value;

const emit = defineEmits<{
(
event: "update:aliasedNodeData",
updatedValue: StringAliasedNodeData,
): void;
(event: "update:selectedLanguage", languageCode: string): void;
(event: "ready"): void;
}>();

Expand All @@ -47,9 +53,13 @@ watchEffect(async () => {

languages.value = response.languages;

const targetLanguageCode =
getOnlyFilledLanguageCode(initialNodeValue) ??
response.request_language;
Comment thread
chrabyrd marked this conversation as resolved.
Resolved

selectedLanguage.value =
languages.value.find((lang: Language) => {
return lang.code === response.request_language;
return lang.code === targetLanguageCode;
}) ??
response.languages.find((lang: Language) => lang.isdefault) ??
response.languages[0];
Expand Down Expand Up @@ -79,6 +89,16 @@ watchEffect(() => {
}
});

watch(
selectedLanguage,
(language) => {
if (language) {
emit("update:selectedLanguage", language.code);
}
},
{ immediate: true },
);

function onUpdateModelValue(updatedValue: string | undefined) {
if (updatedValue === undefined) {
updatedValue = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const { aliasedNodeData, value, mode } = defineProps<TextWidgetProps>();
const emit = defineEmits<{
"update:value": [updatedValue: Record<string, LanguageValue> | null];
"update:aliasedNodeData": [updatedValue: StringAliasedNodeData];
"update:selectedLanguage": [languageCode: string];
initialized: [updatedValue: StringAliasedNodeData];
ready: [];
}>();
Expand Down Expand Up @@ -51,6 +52,7 @@ function onUpdateAliasedNodeData(
:aliased-node-data="resolvedAliasedNodeData"
:render-context="renderContext"
@update:aliased-node-data="onUpdateAliasedNodeData"
@update:selected-language="emit('update:selectedLanguage', $event)"
@ready="emit('ready')"
/>
<TextWidgetViewer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { flushPromises, mount } from "@vue/test-utils";
import { createPinia } from "pinia";
import OpenVue from "openvue/config";
import Select from "openvue/select";

import TextWidgetEditor from "@/arches_vue_components/widgets/TextWidget/components/TextWidgetEditor.vue";

import type { Language } from "@/arches_vue_components/types.ts";

const { ENGLISH, FRENCH } = vi.hoisted(
(): { ENGLISH: Language; FRENCH: Language } => ({
ENGLISH: {
code: "en",
default_direction: "ltr",
id: 1,
isdefault: true,
name: "English",
scope: "system",
},
FRENCH: {
code: "fr",
default_direction: "ltr",
id: 2,
isdefault: false,
name: "French",
scope: "system",
},
}),
);

vi.mock("@/arches_vue_components/widgets/api.ts", () => ({
fetchLanguages: () =>
Promise.resolve({
languages: [ENGLISH, FRENCH],
request_language: ENGLISH.code,
}),
}));

describe("TextWidgetEditor", () => {
beforeEach(() => {
vi.stubGlobal(
"matchMedia",
vi.fn().mockReturnValue({
matches: false,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
}),
);
});

it("reports a language switch without editing the value", async () => {
const wrapper = mount(TextWidgetEditor, {
props: {
aliasedNodeData: {
node_value: {
en: { value: "Barcelona", direction: "ltr" },
},
display_value: "Barcelona",
details: [],
},
},
global: {
plugins: [createPinia(), [OpenVue, { theme: "none" }]],
},
});
await flushPromises();

wrapper.findComponent(Select).vm.$emit("update:modelValue", FRENCH);
await flushPromises();

expect(wrapper.emitted("update:selectedLanguage")).toEqual([
[ENGLISH.code],
[FRENCH.code],
]);
expect(wrapper.emitted("update:aliasedNodeData")).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, onMounted, ref, watchEffect } from "vue";
import { computed, onMounted, ref, watch, watchEffect } from "vue";
import { useGettext } from "vue3-gettext";

import InputText from "openvue/inputtext";
Expand All @@ -8,7 +8,10 @@ import Select from "openvue/select";
import Textarea from "openvue/textarea";

import { MULTILINE_RENDER_CONTEXT } from "@/arches_vue_components/widgets/TextWidget/constants.ts";
import { buildStringAliasedNodeData } from "@/arches_vue_components/datatypes/string/utils.ts";
import {
buildStringAliasedNodeData,
getOnlyFilledLanguageCode,
} from "@/arches_vue_components/datatypes/string/utils.ts";
import { useLanguageStore } from "@/arches_vue_components/stores/useLanguageStore.ts";

import type {
Expand Down Expand Up @@ -37,6 +40,7 @@ const emit = defineEmits<{
event: "update:aliasedNodeData",
updatedValue: StringAliasedNodeData,
): void;
(event: "update:selectedLanguage", languageCode: string): void;
(event: "ready"): void;
}>();

Expand Down Expand Up @@ -85,20 +89,26 @@ watchEffect(() => {
}
});

watch(
selectedLanguage,
(language) => {
if (language) {
emit("update:selectedLanguage", language.code);
}
},
{ immediate: true },
);

onMounted(() => {
if (selectedLanguage.value) {
emit("ready");
}
});

function resolveInitialLanguage(): Language | undefined {
const nonEmptyLanguageCodes = Object.entries(initialNodeValue ?? {})
.filter(([, language]) => language.value !== "")
.map(([code]) => code);
let targetLanguageCode = languageStore.requestLanguageCode;
if (nonEmptyLanguageCodes.length === 1) {
targetLanguageCode = nonEmptyLanguageCodes[0];
}
const targetLanguageCode =
getOnlyFilledLanguageCode(initialNodeValue) ??
languageStore.requestLanguageCode;
Comment thread
chrabyrd marked this conversation as resolved.
Resolved
return (
languageStore.languages.find(
(language: Language) => language.code === targetLanguageCode,
Expand Down
1 change: 1 addition & 0 deletions releases/8.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- `DELETE /api/tiles/<tileid>` lets token-authenticated API clients delete a tile, following the same rules as the resource editor: reviewers may delete any tile, others only tiles that are still provisional [#13013](https://github.com/archesproject/arches/pull/13013)
- Added id, created_time, and last_updated fields to the TileModel and ResourceInstance tables. During migration, the those tables will be locked until the migration related to this change completes. Notifiy users appropriately. [#13019](https://github.com/archesproject/arches/pull/13019)
- Add a graph-wide widget config endpoint, a read-only `MapComponent` mode (`MapWidgetViewer`), and `shouldShowCardName` and `focusWidgetInputForNodeAlias()` to `GenericCard` [#13016](https://github.com/archesproject/arches/pull/13016)
- Text and rich text widgets emit `update:selectedLanguage` with the language their picker shows, and the rich text widget opens on a value's only filled language as the text widget does [#13129](https://github.com/archesproject/arches/issues/13129)

### Performance Improvements

Expand Down
Loading