Fix accept from one file

This commit is contained in:
friebetill 2023-02-07 16:02:23 +08:00
parent 37319e5c5d
commit f0b2c3a20b
2 changed files with 48 additions and 3 deletions

View file

@ -1,6 +1,6 @@
import { TFile } from 'obsidian'
import { Difference } from '../data/difference'
import { deleteLines, replaceLine } from '../utils/string_utils'
import { deleteLines, insertLine, replaceLine } from '../utils/string_utils'
import { ActionLineButton } from './action_line_button'
import { ActionLineDivider } from './action_line_divider'
@ -60,7 +60,7 @@ export class ActionLine {
} else if (hasMinusLines) {
new ActionLineButton({
text: `Accept from ${this.file1.name}`,
onClick: (e) => this.acceptTopClick(e, this.difference),
onClick: (e) => this.insertFile1Difference(e, this.difference),
}).build(actionLine)
ActionLineDivider.build(actionLine)
new ActionLineButton({
@ -70,7 +70,7 @@ export class ActionLine {
} else if (hasPlusLines) {
new ActionLineButton({
text: `Accept from ${this.file2.name}`,
onClick: (e) => this.acceptBottomClick(e, this.difference),
onClick: (e) => this.insertFile2Difference(e, this.difference),
}).build(actionLine)
ActionLineDivider.build(actionLine)
new ActionLineButton({
@ -142,6 +142,40 @@ export class ActionLine {
this.triggerRebuild()
}
private async insertFile1Difference(
event: MouseEvent,
difference: Difference
): Promise<void> {
event.preventDefault()
const changedLines = difference.file1Lines.join('\n')
const newContent = insertLine({
fullText: this.file2Content,
newLine: changedLines,
position: difference.file2Start,
})
await app.vault.modify(this.file2, newContent)
this.triggerRebuild()
}
private async insertFile2Difference(
event: MouseEvent,
difference: Difference
): Promise<void> {
event.preventDefault()
const changedLines = difference.file2Lines.join('\n')
const newContent = insertLine({
fullText: this.file1Content,
newLine: changedLines,
position: difference.file1Start,
})
await app.vault.modify(this.file1, newContent)
this.triggerRebuild()
}
async discardFile1Difference(
event: MouseEvent,
difference: Difference

View file

@ -1,3 +1,14 @@
/** Method to insert a line in a string */
export function insertLine(args: {
fullText: string
newLine: string
position: number
}): string {
const lines = args.fullText.split('\n')
lines.splice(args.position, 0, args.newLine)
return lines.join('\n')
}
/** Method to replace a line in a string */
export function replaceLine(args: {
fullText: string