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
35 changes: 35 additions & 0 deletions packages/core/src/plugins/quick-diff/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class PluginQuickDiff extends BasePlugin {
protected start(): Promise<void> | void {
this.ctx.set('quickDiff', this)
this.ctx.on('quick-edit/wiki-page', this.injectQuickEdit.bind(this))
this.ctx.on('quick-edit/conflict', this.showQuickEditConflict.bind(this))
window.RLQ.push(this.injectHistoryPage.bind(this))
}

Expand Down Expand Up @@ -193,6 +194,40 @@ export class PluginQuickDiff extends BasePlugin {
})
}

/**
* After a quick edit conflict, show what saving the user's text would change in the latest version,
* including edits by others it would remove.
*/
private showQuickEditConflict({
modal,
wikiPage,
options,
text,
}: QuickEditEventPayload & { text: string }) {
// Nothing to compare against: deleted meanwhile, or appending a new section
if (wikiPage.pageid === 0 || options.section === 'new') return
const fromtext = wikiPage.revisions?.[0]?.content || ''
if (fromtext === text) return

const diffModal = this.comparePages(
{
fromtitle: wikiPage.title,
fromtext,
totitle: wikiPage.title,
totext: text,
topst: true,
},
undefined,
{
backdrop: false,
draggable: true,
}
)
modal.on(modal.Event.Close, () => {
diffModal.destroy()
})
}

readonly COMPARE_API_DEFAULT_OPTIONS: Partial<CompareApiRequestOptions> = {
prop: [
'comment',
Expand Down
75 changes: 60 additions & 15 deletions packages/core/src/plugins/quick-edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ declare module '@/InPageEdit' {
'quick-edit/wiki-page'(payload: QuickEditEventPayload): void
'quick-edit/edit-notice'(payload: QuickEditEventPayload & { editNotices: ReactNode[] }): void
'quick-edit/submit'(payload: QuickEditSubmitPayload & { ctx: InPageEdit }): void
/**
* Saving failed because the page changed meanwhile. `wikiPage` already holds the latest version,
* `text` is what the user tried to save.
*/
'quick-edit/conflict'(
payload: QuickEditEventPayload & { text: string; error: MediaWikiApiError }
): void
}
interface PreferencesMap {
'quickEdit.editSummary': string
Expand Down Expand Up @@ -383,7 +390,6 @@ export class PluginQuickEdit extends BasePlugin {
) as HTMLFormElement
modal.setContent(editForm)

let dismissWarnings = false
modal.addButton(
{
side: 'left',
Expand All @@ -407,9 +413,8 @@ export class PluginQuickEdit extends BasePlugin {
minor: formData.get('minor') === 'on',
section: options.section,
watchlist: watchList,
// 如果无视风险,那么就不再尝试解决冲突,直接重建页面
createonly: wikiPage.pageid === 0 && !dismissWarnings,
recreate: wikiPage.pageid === 0 && dismissWarnings,
// Read at submit time: a conflict refresh may have changed whether the page exists
createonly: wikiPage.pageid === 0,
})

modal.setOptions({
Expand All @@ -429,27 +434,49 @@ export class PluginQuickEdit extends BasePlugin {
} catch (error) {
modal.setLoadingState(false)

if (MediaWikiApiError.is(error)) {
if (
error.code === 'pagedeleted' ||
error.code === 'editconflict' ||
error.code === 'articleexists'
) {
dismissWarnings = true
this.ctx.modal.notify('warning', {
if (
MediaWikiApiError.is(error) &&
PluginQuickEdit.CONFLICT_CODES.includes(error.code)
) {
try {
await this.refreshWikiPage(wikiPage, options)
} catch (refreshError) {
this.ctx.modal.notify('error', {
title: $`Submission Error`,
content: (
<div>
<p>
<strong>{error.message}</strong>
</p>
<p>{$`You can try to submit again to dismiss the warnings.`}</p>
<p>{$`Failed to load the latest version of the page.`}</p>
</div>
),
closeAfter: 15 * 1000,
})
return // can be dismissed by re-submission
return
}
this.ctx.modal.notify('warning', {
title: $`Edit Conflict`,
content: (
<div>
<p>
<strong>{error.message}</strong>
</p>
<p>
{$`The latest version has been loaded. Review the differences; submitting again will save your text over the latest version.`}
</p>
</div>
),
closeAfter: 15 * 1000,
})
this.ctx.emit('quick-edit/conflict', {
ctx: this.ctx,
options,
modal,
wikiPage,
text: (formData.get('text') as string) ?? '',
error,
})
return
}

// Errors not handled above
Expand Down Expand Up @@ -552,6 +579,24 @@ export class PluginQuickEdit extends BasePlugin {
}
}

/**
* MediaWiki errors meaning the page changed after it was loaded:
* edited (editconflict), created (articleexists) or deleted (pagedeleted).
*/
static readonly CONFLICT_CODES = ['editconflict', 'articleexists', 'pagedeleted']

/**
* Load the latest version into `wikiPage` in place, so every holder of the instance
* (diff button, unsaved-changes check, next submit) works against it.
*/
async refreshWikiPage(wikiPage: IWikiPage, options: QuickEditOptions) {
const section = typeof options.section === 'number' ? options.section : undefined
const latest = await this.ctx.wikiPage.newFromTitle(wikiPage.title, false, section)
wikiPage.pageInfo = latest.pageInfo
wikiPage.fetchedAt = latest.fetchedAt
return wikiPage
}

async getWikiPageFromPayload(payload: Partial<QuickEditOptions>) {
if (payload.revision) {
return this.ctx.wikiPage.newFromRevision(payload.revision, payload.section)
Expand Down