Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
90 changes: 61 additions & 29 deletions WebUI/src/main/ts/editor/EditorHost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ import {
} from "./editorCreate";
import { editorBinaryErrorReason, isImageFile } from "./editorBinary";
import { editorSaveErrorReason } from "./editorSave";
import {
fieldsForEditorSave,
schemaReadOnlyFieldNames,
} from "./editorReadOnlySave";
import {
changePageTemplate,
isAllowedPageTemplate,
Expand Down Expand Up @@ -801,6 +805,10 @@ export function EditorHost({

const [payload, setPayload] = useState<ItemEditorFields | null>(null);
const [schema, setSchema] = useState<ContentTypeFieldSummary[]>([]);
const schemaReadOnlyNames = useMemo(
() => schemaReadOnlyFieldNames(schema),
[schema],
);
const [allowedTemplateCount, setAllowedTemplateCount] = useState(0);
const [loadedAllowedTemplates, setLoadedAllowedTemplates] = useState<
NamedObjectRef[] | null
Expand Down Expand Up @@ -991,7 +999,13 @@ export function EditorHost({
useEffect(() => {
const onBeforeUnload = (event: BeforeUnloadEvent) => {
if (
!editorDraftIsDirty(payload?.fields, draft, pendingFiles, pendingClears)
!editorDraftIsDirty(
payload?.fields,
draft,
pendingFiles,
pendingClears,
schemaReadOnlyNames,
)
) {
return;
}
Expand All @@ -1000,7 +1014,7 @@ export function EditorHost({
};
window.addEventListener("beforeunload", onBeforeUnload);
return () => window.removeEventListener("beforeunload", onBeforeUnload);
}, [payload, draft, pendingFiles, pendingClears]);
}, [payload, draft, pendingFiles, pendingClears, schemaReadOnlyNames]);

useEffect(() => {
// Drop binaries from the previous item or mode. A confirmed leave and a
Expand Down Expand Up @@ -1221,12 +1235,17 @@ export function EditorHost({
if (!payload) {
return [];
}
const storedByName = new Map(
payload.fields.map((field) => [field.name, fieldValueAsString(field.value)]),
);
return mergeEditorRows(
{
...payload,
fields: payload.fields.map((f) => ({
name: f.name,
value: fieldValueAsString(draft[f.name] ?? f.value),
value: schemaReadOnlyNames.has(f.name)
? (storedByName.get(f.name) ?? "")
: fieldValueAsString(draft[f.name] ?? f.value),
})),
},
schema,
Expand All @@ -1240,9 +1259,11 @@ export function EditorHost({
)
.map((row) => ({
...row,
value: fieldValueAsString(draft[row.name] ?? row.value),
value: row.readOnly
? (storedByName.get(row.name) ?? "")
: fieldValueAsString(draft[row.name] ?? row.value),
}));
}, [payload, draft, schema]);
}, [payload, draft, schema, schemaReadOnlyNames]);

useEffect(() => {
const errors = focusInvalidRef.current;
Expand Down Expand Up @@ -1270,6 +1291,9 @@ export function EditorHost({
}

function setField(name: string, value: string): void {
if (schemaReadOnlyNames.has(name)) {
return;
}
setDraft((prev) => ({ ...prev, [name]: value }));
setFieldErrors((prev) => {
if (!(name in prev)) {
Expand All @@ -1282,6 +1306,9 @@ export function EditorHost({
}

function setFile(name: string, file: File | null): void {
if (schemaReadOnlyNames.has(name)) {
return;
}
setPendingFiles((prev) => {
const next = { ...prev };
if (file) {
Expand All @@ -1304,6 +1331,9 @@ export function EditorHost({
}

function markBinaryClear(name: string, hadStored: boolean): void {
if (schemaReadOnlyNames.has(name)) {
return;
}
setPendingFiles((prev) => {
if (!(name in prev)) {
return prev;
Expand All @@ -1329,7 +1359,7 @@ export function EditorHost({
name: row.name,
kind: row.kind,
required: row.required,
value: fieldValueAsString(draft[row.name] ?? row.value),
value: row.value,
})),
pendingFiles,
message(EDITOR_MSG.FIELD_REQUIRED),
Expand All @@ -1351,7 +1381,7 @@ export function EditorHost({
name: row.name,
kind: row.kind,
required: row.required,
value: fieldValueAsString(draft[row.name] ?? row.value),
value: row.value,
})),
message(EDITOR_MSG.FIELD_INVALID_DATE),
);
Expand All @@ -1373,7 +1403,7 @@ export function EditorHost({
rows.map((row) => ({
name: row.name,
kind: row.kind,
value: fieldValueAsString(draft[row.name] ?? row.value),
value: row.value,
})),
message(EDITOR_MSG.HTML_UNSAFE),
);
Expand All @@ -1388,7 +1418,7 @@ export function EditorHost({
rows.map((row) => ({
name: row.name,
kind: row.kind,
value: fieldValueAsString(draft[row.name] ?? row.value),
value: row.value,
})),
message(EDITOR_MSG.LONGTEXT_INVALID),
);
Expand All @@ -1402,7 +1432,7 @@ export function EditorHost({
rows.map((row) => ({
name: row.name,
kind: row.kind,
value: fieldValueAsString(draft[row.name] ?? row.value),
value: row.value,
numericInteger: row.numericInteger,
numericMinimum: row.numericMinimum,
numericMaximum: row.numericMaximum,
Expand All @@ -1421,7 +1451,7 @@ export function EditorHost({
rows.map((row) => ({
name: row.name,
kind: row.kind,
value: fieldValueAsString(draft[row.name] ?? row.value),
value: row.value,
})),
message(EDITOR_MSG.LINK_INVALID),
);
Expand All @@ -1436,7 +1466,7 @@ export function EditorHost({
name: row.name,
kind: row.kind,
label: row.label,
value: fieldValueAsString(draft[row.name] ?? row.value),
value: row.value,
})),
keywordChoicesRef.current,
keywordOutsideCatalogMessage,
Expand Down Expand Up @@ -1464,21 +1494,7 @@ export function EditorHost({
const itemId = String(contentId);
const next: ItemEditorFields = {
...payload,
fields: rows
.filter((row) => row.kind !== "file" && row.kind !== "image")
.map((row) => ({
name: row.name,
value: fieldValueAsString(draft[row.name] ?? row.value),
...(row.kind === "number"
? {
dataType: row.numericInteger === false ? "float" : "integer",
minimum: row.numericMinimum,
maximum: row.numericMaximum,
}
: row.kind === "link"
? { dataType: "link" }
: {}),
})),
fields: fieldsForEditorSave(rows),
};
if (
isExplorerPageType(payload.contentType) &&
Expand Down Expand Up @@ -2556,7 +2572,15 @@ export function EditorHost({
setPreviewErrorKey(EDITOR_MSG.PREVIEW_UNAVAILABLE);
return;
}
if (editorDraftIsDirty(payload?.fields, draft, pendingFiles, pendingClears)) {
if (
editorDraftIsDirty(
payload?.fields,
draft,
pendingFiles,
pendingClears,
schemaReadOnlyNames,
)
) {
const confirmFn =
confirmUnsavedPreview ??
((body: string) =>
Expand Down Expand Up @@ -2692,7 +2716,15 @@ export function EditorHost({
}

function allowLeave(): boolean {
if (!editorDraftIsDirty(payload?.fields, draft, pendingFiles, pendingClears)) {
if (
!editorDraftIsDirty(
payload?.fields,
draft,
pendingFiles,
pendingClears,
schemaReadOnlyNames,
)
) {
return true;
}
const confirmFn =
Expand Down
5 changes: 5 additions & 0 deletions WebUI/src/main/ts/editor/editorPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export function editorDraftIsDirty(
draft: Record<string, string>,
pendingFiles: Record<string, File>,
pendingClears?: Record<string, boolean>,
/** Schema read-only names. A client edit of those fields is not an unsaved change. */
ignoredNames?: ReadonlySet<string>,
): boolean {
if (Object.keys(pendingFiles).length > 0) {
return true;
Expand All @@ -96,6 +98,9 @@ export function editorDraftIsDirty(
return false;
}
for (const field of saved) {
if (ignoredNames?.has(field.name)) {
continue;
}
const current = Object.prototype.hasOwnProperty.call(draft, field.name)
? draft[field.name]
: fieldAsString(field.value);
Expand Down
76 changes: 76 additions & 0 deletions WebUI/src/main/ts/editor/editorReadOnlySave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2026 Intersoft Data Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { ItemEditorField } from "./itemFieldsApi";

/**
* Content-type fields marked read-only (#5345). The editor PUT omits them so a
* client edit cannot replace the stored value. File and image widgets stay on
* the binary API and are omitted here as well.
*/
export interface EditorSaveFieldRow {
name: string;
kind: string;
readOnly?: boolean;
value: string;
numericInteger?: boolean;
numericMinimum?: string;
numericMaximum?: string;
}

export function schemaReadOnlyFieldNames(
schema: readonly { name?: string; readOnly?: boolean }[] | undefined,
): ReadonlySet<string> {
const names = new Set<string>();
if (!schema) {
return names;
}
for (const field of schema) {
const name = (field.name ?? "").trim();
if (name && field.readOnly === true) {
names.add(name);
}
}
return names;
}

export function fieldsForEditorSave(
rows: readonly EditorSaveFieldRow[],
): ItemEditorField[] {
const fields: ItemEditorField[] = [];
for (const row of rows) {
if (row.readOnly === true || row.kind === "file" || row.kind === "image") {
continue;
}
if (row.kind === "number") {
fields.push({
name: row.name,
value: row.value,
dataType: row.numericInteger === false ? "float" : "integer",
minimum: row.numericMinimum,
maximum: row.numericMaximum,
});
continue;
}
if (row.kind === "link") {
fields.push({ name: row.name, value: row.value, dataType: "link" });
continue;
}
fields.push({ name: row.name, value: row.value });
}
return fields;
}
Loading
Loading