fix(editor): citation notices, literal citation text, local limits
This commit is contained in:
1 parent
c93787c947
commit
3e94f732c7
5 files changed
+152
-61
No files matched your search
@@ -41,50 +41,58 @@ const isClash = (outcome: CommitOutcome) =>
|
||||
outcome.reason === 'core' &&
|
||||
outcome.failure.kind === 'duplicate_key'
|
||||
|
||||
type Settle = (field: string, outcome: CommitOutcome) => boolean
|
||||
|
||||
const attempt = (command: EditCommand) =>
|
||||
useEditorStore.getState().commit(command)
|
||||
|
||||
function insertNew(
|
||||
settle: Settle,
|
||||
field: string,
|
||||
prefix: IdPrefix,
|
||||
build: (id: string) => EditCommand,
|
||||
): boolean {
|
||||
let outcome = attempt(build(newElementId(prefix)))
|
||||
if (isClash(outcome)) outcome = attempt(build(newElementId(prefix)))
|
||||
return settle(field, outcome)
|
||||
}
|
||||
|
||||
function commitBatch(
|
||||
settle: Settle,
|
||||
field: string,
|
||||
build: () => readonly EditCommand[],
|
||||
): boolean {
|
||||
const store = useEditorStore.getState()
|
||||
let outcome = store.commitBatch(build())
|
||||
if (isClash(outcome)) outcome = store.commitBatch(build())
|
||||
return settle(field, outcome)
|
||||
}
|
||||
|
||||
function dryRun(settle: Settle, field: string, command: EditCommand): boolean {
|
||||
const text = useEditorStore.getState().documentText
|
||||
const edited = applyEdit(text, command)
|
||||
const outcome: CommitOutcome = edited.ok
|
||||
? { ok: true }
|
||||
: { ok: false, reason: 'core', failure: edited }
|
||||
return settle(field, outcome)
|
||||
}
|
||||
|
||||
/** The write path of an editor panel: commit once, keep the refusal next to the control. */
|
||||
export function useEditCommand(): EditCommandApi {
|
||||
const t = useT()
|
||||
const [error, setError] = useState<FieldError | null>(null)
|
||||
|
||||
const attempt = (command: EditCommand) =>
|
||||
useEditorStore.getState().commit(command)
|
||||
|
||||
const run = (field: string, command: EditCommand): boolean =>
|
||||
settle(field, attempt(command))
|
||||
|
||||
const settle = (field: string, outcome: CommitOutcome): boolean => {
|
||||
const settle: Settle = (field, outcome) => {
|
||||
setError(outcome.ok ? null : { field, message: refusalMessage(t, outcome) })
|
||||
return outcome.ok
|
||||
}
|
||||
|
||||
const runNew: EditCommandApi['runNew'] = (field, prefix, build) => {
|
||||
let outcome = attempt(build(newElementId(prefix)))
|
||||
if (isClash(outcome)) outcome = attempt(build(newElementId(prefix)))
|
||||
return settle(field, outcome)
|
||||
}
|
||||
|
||||
const runBatch: EditCommandApi['runBatch'] = (field, build) => {
|
||||
const store = useEditorStore.getState()
|
||||
let outcome = store.commitBatch(build())
|
||||
if (isClash(outcome)) outcome = store.commitBatch(build())
|
||||
return settle(field, outcome)
|
||||
}
|
||||
|
||||
const check: EditCommandApi['check'] = (field, command) => {
|
||||
const text = useEditorStore.getState().documentText
|
||||
const edited = applyEdit(text, command)
|
||||
const outcome: CommitOutcome = edited.ok
|
||||
? { ok: true }
|
||||
: { ok: false, reason: 'core', failure: edited }
|
||||
return settle(field, outcome)
|
||||
}
|
||||
|
||||
return {
|
||||
fieldError: (field) => (error?.field === field ? error.message : undefined),
|
||||
run,
|
||||
runNew,
|
||||
runBatch,
|
||||
check,
|
||||
run: (field, command) => settle(field, attempt(command)),
|
||||
runNew: (field, prefix, build) => insertNew(settle, field, prefix, build),
|
||||
runBatch: (field, build) => commitBatch(settle, field, build),
|
||||
check: (field, command) => dryRun(settle, field, command),
|
||||
reject: (field, messageKey) => {
|
||||
setError({ field, message: t(messageKey) })
|
||||
return false
|
||||
|
||||
Reference in new issue
Block a user