Skip to content

Commit

Permalink
using more stable key for importing/exporting word forms globally - c…
Browse files Browse the repository at this point in the history
…oncatenated base forms like "sein:de;ser:es", #254
  • Loading branch information
klues committed Mar 11, 2024
1 parent cd21a00 commit 76561bc
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions src/vue-components/modals/editElementWordForms.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
import {util} from "../../js/util/util.js";
import {i18nService} from "../../js/service/i18nService.js";
import {dataService} from "../../js/service/data/dataService.js";
import {constants} from "../../js/util/constants.js";
export default {
components: {Accordion, EditWordForm},
props: ['gridElement', 'gridData'],
Expand Down Expand Up @@ -194,20 +195,29 @@
this.allGrids = this.allGrids || (await dataService.getGrids(true));
let baseMap = {}; // base -> list of word form objects
for (let newForm of importForms) {
baseMap[newForm.base] = baseMap[newForm.base] || [];
baseMap[newForm.base].push(newForm);
let baseString = newForm.base || "";
let bases = baseString.split(";");
for (let base of bases) {
baseMap[base] = baseMap[base] || [];
baseMap[base].push(newForm);
}
}
let ownGridChanged = false;
for (let grid of this.allGrids) {
let changed = false;
let changedGrid = false;
for (let element of grid.gridElements) {
let baseValue = i18nService.getTranslation(element.label);
if (baseMap[baseValue]) {
element.wordForms = baseMap[baseValue];
changed = true;
let allNewWordForms = new Set();
let baseStrings = this.getBaseStringsFromWordForms(element.wordForms);
for (let baseString of baseStrings) {
if (baseMap[baseString]) {
allNewWordForms = new Set([...allNewWordForms, ...baseMap[baseString]]);
}
}
let newArray = Array.from(allNewWordForms);
changedGrid = changedGrid || element.wordForms.length !== newArray.length || JSON.stringify(element.wordForms) !== JSON.stringify(newArray);
element.wordForms = newArray;
}
if (changed) {
if (changedGrid) {
this.gridPasteCount++;
await dataService.saveGrid(grid);
if (grid.id === this.gridData.id) {
Expand Down Expand Up @@ -238,22 +248,33 @@
let alreadyCopied = {}; // base -> tags
for (let element of elements) {
let forms = element.wordForms || [];
let baseStrings = this.getBaseStringsFromWordForms(forms);
let baseFormsString = baseStrings.join(";");
for (let form of forms) {
let tags = JSON.stringify(form.tags).replaceAll('"', '').replaceAll("'", "").replaceAll("[", "").replaceAll("]", "").replaceAll(",", ", ");
let lang = form.lang || '';
let pronunciation = form.pronunciation || '';
let base = i18nService.getTranslation(element.label);
let key = tags + lang;
let key = baseFormsString + tags + lang;
alreadyCopied[key] = alreadyCopied[key] || [];
if (!this.importExportGlobally || (base && !alreadyCopied[key].includes(tags))) {
if (!this.importExportGlobally || (baseFormsString && !alreadyCopied[key].includes(tags))) {
alreadyCopied[key].push(tags);
copyString += `${form.value}\t${lang}\t${tags}\t${base}\t${pronunciation}\n`;
copyString += `${form.value}\t${lang}\t${tags}\t${baseFormsString}\t${pronunciation}\n`;
this.msgCount++;
}
}
}
util.copyToClipboard(copyString);
this.currentMsg = this.msgTypes.SUCCESS_COPY;
},
/**
* returns a list of base form strings in form of "<baseForm>:<lang>" from a given list of word forms.
* @param wordForms
* @return list of base form strings, e.g. ["sein:de", "be:en", "ser:es", ...]
*/
getBaseStringsFromWordForms (forms) {
forms = forms || [];
let baseForms = forms.filter(form => form.tags.includes(constants.WORDFORM_TAG_BASE));
return baseForms.map(form => `${form.value}:${form.lang}`);
}
},
mounted() {
Expand Down

0 comments on commit 76561bc

Please sign in to comment.