Skip to content

Commit

Permalink
fix: properly delete field
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed May 15, 2024
1 parent c2e8a65 commit dffb6ef
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/super-tag/widgets/utils/formOnChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function formOnChange(currentTiddlerTitle?: string, editor?: JSONEditor.J
if (currentTiddlerTitle === undefined || editor === undefined) return;
const latestFormValue = editor?.getValue() as Record<string, unknown>;
if (latestFormValue === undefined || latestFormValue === null) {
return;
return;
}
// Get an array of errors from the validator
const errors = editor.validate();
Expand All @@ -13,22 +13,22 @@ export function formOnChange(currentTiddlerTitle?: string, editor?: JSONEditor.J
errorValidatorInfoElement.className = 'label label-warning';
errorValidatorInfoElement.textContent = 'Form not valid';
}
return
} else {
const tiddlerFields = $tw.wiki.getTiddler(currentTiddlerTitle)?.fields ?? ({} as Record<string, unknown>);
}
let tiddlerFields = $tw.wiki.getTiddler(currentTiddlerTitle)?.fields ?? ({} as Record<string, unknown>);

let hasChange = false;
Object.keys(latestFormValue).forEach((key) => {
if (tiddlerFields[key] !== latestFormValue[key]) {
hasChange = true;
// if user delete the value from the form, also delete the field.
if (tiddlerFields[key] && !latestFormValue[key]) {
delete tiddlerFields[key];
}
let hasChange = false;
Object.keys(latestFormValue).forEach((key) => {
if (tiddlerFields[key] !== latestFormValue[key]) {
hasChange = true;
// if user delete the value from the form, also delete the field.
if (tiddlerFields[key] !== undefined && !latestFormValue[key]) {
const { [key]: _, ...tiddlerFieldsWithoutKey } = tiddlerFields;
tiddlerFields = tiddlerFieldsWithoutKey;
delete latestFormValue[key];
}
});
if (hasChange) {
$tw.wiki.addTiddler({ ...tiddlerFields, ...latestFormValue, title: currentTiddlerTitle });
}
});
if (hasChange) {
$tw.wiki.addTiddler({ ...tiddlerFields, ...latestFormValue, title: currentTiddlerTitle });
}
}

0 comments on commit dffb6ef

Please sign in to comment.