Skip to content

Commit ac0e951

Browse files
committed
Related fixes
Cleaning up unwanted -style classes, fetching content when setting language of an element that already has a field type, using wrapper for async DOM changes
1 parent 230d30f commit ac0e951

1 file changed

Lines changed: 113 additions & 22 deletions

File tree

src/BloomBrowserUI/bookEdit/js/CanvasElementContextControls.tsx

Lines changed: 113 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ import {
7171
kBloomButtonClass,
7272
kBloomCanvasSelector,
7373
} from "../toolbox/canvas/canvasElementUtils";
74-
import { getString, post, useApiObject } from "../../utils/bloomApi";
74+
import { wrapWithRequestPageContentDelay } from "./bloomEditing";
75+
import { get, post, useApiObject } from "../../utils/bloomApi";
7576
import { ILanguageNameValues } from "../bookSettings/FieldVisibilityGroup";
7677
import OverflowChecker from "../OverflowChecker/OverflowChecker";
7778

@@ -949,6 +950,66 @@ function adjustAutoSizeForVisibleEditableInTranslationGroup(tg: HTMLElement) {
949950
OverflowChecker.AdjustSizeOrMarkOverflow(visibleEditable);
950951
}
951952

953+
function setEditableContentFromKnownDataBookValueIfAny(
954+
editable: HTMLElement,
955+
dataBook: string | null,
956+
tg: HTMLElement,
957+
) {
958+
if (!dataBook) {
959+
return;
960+
}
961+
wrapWithRequestPageContentDelay(
962+
() =>
963+
new Promise<void>((resolve, reject) => {
964+
get(
965+
`editView/getDataBookValue?lang=${editable.getAttribute("lang")}&dataBook=${dataBook}`,
966+
(result) => {
967+
try {
968+
const content = result.data;
969+
// content comes from a source that looked empty, we don't want to overwrite something the user may
970+
// already have typed here.
971+
// But it may well have something in it, because we usually have an empty paragraph to start with.
972+
// To test whether it looks empty, we put the text into a newly created element and then
973+
// see whether it's textContent is empty.
974+
// The logic of overwriting something which the user has typed here is that if we keep what's here,
975+
// then the user may never know that there was already something in that field. But if we overwrite, then
976+
// the user can always correct it back to what he just typed.
977+
const temp = document.createElement("div");
978+
temp.innerHTML = content || "";
979+
if (temp.textContent.trim() !== "")
980+
editable.innerHTML = content;
981+
adjustAutoSizeForVisibleEditableInTranslationGroup(
982+
tg,
983+
);
984+
resolve();
985+
} catch (error) {
986+
reject(error);
987+
}
988+
},
989+
(error) => {
990+
reject(error);
991+
},
992+
);
993+
}),
994+
"setCanvasFieldValueFromDataBook",
995+
);
996+
}
997+
998+
function applyAppearanceClassForEditable(editable: HTMLElement) {
999+
editable.classList.remove(
1000+
"bloom-contentFirst",
1001+
"bloom-contentSecond",
1002+
"bloom-contentThird",
1003+
);
1004+
if (editable.classList.contains("bloom-content1")) {
1005+
editable.classList.add("bloom-contentFirst");
1006+
} else if (editable.classList.contains("bloom-contentNational1")) {
1007+
editable.classList.add("bloom-contentSecond");
1008+
} else if (editable.classList.contains("bloom-contentNational2")) {
1009+
editable.classList.add("bloom-contentThird");
1010+
}
1011+
}
1012+
9521013
function makeLanguageMenuItem(
9531014
ce: HTMLElement,
9541015
menuOptions: IMenuItemWithSubmenu[],
@@ -973,7 +1034,7 @@ function makeLanguageMenuItem(
9731034
tg.setAttribute("data-default-languages", dataDefaultLang);
9741035
const editables = Array.from(
9751036
tg.getElementsByClassName("bloom-editable"),
976-
);
1037+
) as HTMLElement[];
9771038
if (editables.length === 0) return; // not able to handle this yet.
9781039
let editableInLang = editables.find(
9791040
(e) => e.getAttribute("lang") === langCode,
@@ -1024,6 +1085,12 @@ function makeLanguageMenuItem(
10241085
}
10251086
}
10261087

1088+
setEditableContentFromKnownDataBookValueIfAny(
1089+
editableInLang,
1090+
dataBookValue,
1091+
tg,
1092+
);
1093+
10271094
// and conversely remove them from the others
10281095
for (const editable of editables) {
10291096
// Ensure visibility code is off for others.
@@ -1195,6 +1262,37 @@ function makeFieldTypeMenuItem(
11951262
}
11961263
};
11971264

1265+
const removeConflictingStyleClasses = (
1266+
fieldType: {
1267+
editableClasses: string[];
1268+
classes: string[];
1269+
},
1270+
editables: HTMLElement[],
1271+
) => {
1272+
const newStyleClasses = new Set(
1273+
[...fieldType.classes, ...fieldType.editableClasses].filter((c) =>
1274+
c.endsWith("-style"),
1275+
),
1276+
);
1277+
if (newStyleClasses.size === 0) {
1278+
return;
1279+
}
1280+
1281+
const stripStyleClasses = (element: HTMLElement) => {
1282+
Array.from(element.classList).forEach((className) => {
1283+
if (
1284+
className.endsWith("-style") &&
1285+
!newStyleClasses.has(className)
1286+
) {
1287+
element.classList.remove(className);
1288+
}
1289+
});
1290+
};
1291+
1292+
stripStyleClasses(tg);
1293+
editables.forEach((editable) => stripStyleClasses(editable));
1294+
};
1295+
11981296
const activeType = tg
11991297
.getElementsByClassName("bloom-editable bloom-visibility-code-on")[0]
12001298
?.getAttribute("data-book");
@@ -1223,7 +1321,7 @@ function makeFieldTypeMenuItem(
12231321
clearFieldTypeClasses();
12241322
const editables = Array.from(
12251323
tg.getElementsByClassName("bloom-editable"),
1226-
);
1324+
) as HTMLElement[];
12271325
if (fieldType.readOnly) {
12281326
const readOnlyDiv = document.createElement("div");
12291327
readOnlyDiv.setAttribute(
@@ -1245,34 +1343,27 @@ function makeFieldTypeMenuItem(
12451343
// Reload the page to get the derived content loaded.
12461344
post("common/saveChangesAndRethinkPageEvent", () => {});
12471345
} else {
1346+
removeConflictingStyleClasses(fieldType, editables);
12481347
tg.classList.add(...fieldType.classes);
12491348
for (const editable of editables) {
12501349
editable.classList.add(...fieldType.editableClasses);
12511350
editable.setAttribute("data-book", fieldType.dataBook);
1351+
if (
1352+
fieldsControlledByAppearanceSystem.includes(
1353+
fieldType.dataBook,
1354+
)
1355+
) {
1356+
applyAppearanceClassForEditable(editable);
1357+
}
12521358
if (
12531359
editable.classList.contains(
12541360
"bloom-visibility-code-on",
12551361
)
12561362
) {
1257-
getString(
1258-
`editView/getDataBookValue?lang=${editable.getAttribute("lang")}&dataBook=${fieldType.dataBook}`,
1259-
(content) => {
1260-
// content comes from a source that looked empty, we don't want to overwrite something the user may
1261-
// already have typed here.
1262-
// But it may well have something in it, because we usually have an empty paragraph to start with.
1263-
// To test whether it looks empty, we put the text into a newly created element and then
1264-
// see whether it's textContent is empty.
1265-
// The logic of overwriting something which the user has typed here is that if we keep what's here,
1266-
// then the user may never know that there was already something in that field. But if we overwrite, then
1267-
// the user can always correct it back to what he just typed.
1268-
const temp = document.createElement("div");
1269-
temp.innerHTML = content || "";
1270-
if (temp.textContent.trim() !== "")
1271-
editable.innerHTML = content;
1272-
adjustAutoSizeForVisibleEditableInTranslationGroup(
1273-
tg,
1274-
);
1275-
},
1363+
setEditableContentFromKnownDataBookValueIfAny(
1364+
editable,
1365+
fieldType.dataBook,
1366+
tg,
12761367
);
12771368
}
12781369
}

0 commit comments

Comments
 (0)