From f0b2c3a20b501f4f9dc9b3ddfc720aafc021fe65 Mon Sep 17 00:00:00 2001 From: friebetill Date: Tue, 7 Feb 2023 16:02:23 +0800 Subject: [PATCH] Fix accept from one file --- src/components/action_line.ts | 40 ++++++++++++++++++++++++++++++++--- src/utils/string_utils.ts | 11 ++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/components/action_line.ts b/src/components/action_line.ts index 692eda6..efa903a 100644 --- a/src/components/action_line.ts +++ b/src/components/action_line.ts @@ -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 { + 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 { + 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 diff --git a/src/utils/string_utils.ts b/src/utils/string_utils.ts index d0ee754..d026825 100644 --- a/src/utils/string_utils.ts +++ b/src/utils/string_utils.ts @@ -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