Keep a continued draft on screen, ask before Edit drops rewritten copy, and retry clearing the draft after publish.
This commit is contained in:
1 parent
54a4871ad1
commit
7c08766092
11 files changed
+361
-41
No files matched your search
@@ -1,6 +1,9 @@
|
||||
import type { CreateFlowState } from "../../app/(app)/create/types";
|
||||
import { createFlowStateHasKeys } from "./draftHydrationUtils";
|
||||
import { PUBLISHED_CUSTOM_RULE_SELECTION_KEYS } from "./customRuleFacets";
|
||||
import {
|
||||
CUSTOM_RULE_FACETS,
|
||||
PUBLISHED_CUSTOM_RULE_SELECTION_KEYS,
|
||||
} from "./customRuleFacets";
|
||||
import type { StoredLastPublishedRule } from "./lastPublishedRule";
|
||||
import {
|
||||
createFlowStateFromPublishedRule,
|
||||
@@ -17,19 +20,86 @@ function sortedIds(value: unknown): string {
|
||||
.join(",");
|
||||
}
|
||||
|
||||
function trimmedText(value: unknown): string {
|
||||
return typeof value === "string" ? value.trim() : "";
|
||||
}
|
||||
|
||||
function canonical(value: unknown): unknown {
|
||||
if (Array.isArray(value)) return value.map(canonical);
|
||||
if (value && typeof value === "object") {
|
||||
const record = value as Record<string, unknown>;
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const key of Object.keys(record).sort()) {
|
||||
const child = record[key];
|
||||
if (child === undefined) continue;
|
||||
out[key] = canonical(child);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/** Missing and empty objects are the same: the author has not written that copy. */
|
||||
function sameCopy(a: unknown, b: unknown): boolean {
|
||||
const normalize = (value: unknown): unknown => {
|
||||
if (value == null) return null;
|
||||
if (
|
||||
typeof value === "object" &&
|
||||
!Array.isArray(value) &&
|
||||
Object.keys(value as object).length === 0
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
};
|
||||
return (
|
||||
JSON.stringify(canonical(normalize(a))) ===
|
||||
JSON.stringify(canonical(normalize(b)))
|
||||
);
|
||||
}
|
||||
|
||||
export function createFlowStateMatchesPublishedRule(
|
||||
state: CreateFlowState,
|
||||
last: StoredLastPublishedRule,
|
||||
): boolean {
|
||||
const patch = createFlowStateFromPublishedRule(last);
|
||||
const title =
|
||||
typeof state.title === "string" ? state.title.trim() : "";
|
||||
const patchTitle =
|
||||
typeof patch.title === "string" ? patch.title.trim() : "";
|
||||
const title = typeof state.title === "string" ? state.title.trim() : "";
|
||||
const patchTitle = typeof patch.title === "string" ? patch.title.trim() : "";
|
||||
if (title !== patchTitle) return false;
|
||||
if (
|
||||
trimmedText(state.communityContext) !== trimmedText(patch.communityContext)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
if (trimmedText(state.summary) !== trimmedText(patch.summary)) {
|
||||
return false;
|
||||
}
|
||||
for (const key of PUBLISHED_CUSTOM_RULE_SELECTION_KEYS) {
|
||||
if (sortedIds(state[key]) !== sortedIds(patch[key])) return false;
|
||||
}
|
||||
for (const row of CUSTOM_RULE_FACETS) {
|
||||
if (
|
||||
!sameCopy(
|
||||
state[row.detailOverridesStateKey],
|
||||
patch[row.detailOverridesStateKey],
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (
|
||||
!sameCopy(
|
||||
state.customMethodCardFieldBlocksById,
|
||||
patch.customMethodCardFieldBlocksById,
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
!sameCopy(state.customMethodCardMetaById, patch.customMethodCardMetaById)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user