diff --git a/src-tauri/src/file_handler.rs b/src-tauri/src/file_handler.rs index 50892a4..65e1faa 100644 --- a/src-tauri/src/file_handler.rs +++ b/src-tauri/src/file_handler.rs @@ -2,12 +2,31 @@ use crate::{ config::{get_open_file_path, set_open_file_path}, get_basepath, }; +use diff_match_patch_rs::{Compat, DiffMatchPatch, Error, PatchInput}; use std::fs::read_to_string; use std::path::Path; use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url}; + +fn compare_content(old_content: &str, new_content: &str) -> Result { + // initializing the module + let dmp = DiffMatchPatch::new(); + // create a list of diffs + let diffs = dmp.diff_main::(old_content, new_content)?; + // Now, we are going to create a list of `patches` to be applied to the old text to get the new text + let patches = dmp.patch_make(PatchInput::new_diffs(&diffs))?; + // in the real world you are going to transmit or store this diff serialized to undiff format to be consumed or used somewhere elese + let patch_txt = dmp.patch_to_text(&patches); + + Ok(patch_txt) +} + #[tauri::command] pub async fn save_file(app_handle: tauri::AppHandle, content: String) { - println!("{:?}", get_open_file_path(app_handle)); + if let Some(path) = get_open_file_path(app_handle.clone()) { + if let Ok(file_content) = load_file(app_handle, path).await { + println!("{:?}", compare_content(&file_content, &content)); + } + } } #[tauri::command] diff --git a/src/filesystem.js b/src/filesystem.js index 526a6f7..4e483b0 100644 --- a/src/filesystem.js +++ b/src/filesystem.js @@ -18,7 +18,7 @@ export function save_file() { console.log(selected_file); var tag_id = document.getElementById("markdown_input"); - invoke("save_file", {content: tag_id.innerHTML}).then((ret) => { + invoke("save_file", {content: tag_id.innerText}).then((ret) => { }); } document.getElementById("save-file").onclick = function () {