From 79ce2019d8b18e1142eef48e556fad077cd5c693 Mon Sep 17 00:00:00 2001 From: Luka Kresoja <57074484+LukeOnuke@users.noreply.github.com> Date: Sun, 21 Nov 2021 00:49:59 +0100 Subject: [PATCH] Fix autosave, recent files not updating, lot more --- README.md | 34 +++++++-------- .../lukeonuke/lmark/gui/MainAppWindow.java | 41 ++++++++++++------- .../com/lukeonuke/lmark/gui/StartWindow.java | 17 +------- .../com/lukeonuke/lmark/util/FileUtils.java | 22 ++++++---- 4 files changed, 60 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 157ae04..f694cae 100644 --- a/README.md +++ b/README.md @@ -5,17 +5,17 @@ ### Main features include - Export markdown to ***pdf*** and ***html***! -- System synchronisable dark / light mode swich +- System synchronisable dark / light mode +- Native integration *(open with, and more)*. - Multiplatform support *(windows, linux, macos)* -- Word like writing expirience - Open source and free - Desktop built application, made with enterprise technologies. - Supports all scripts (chirilic, latin, Arabic, Japanese). ### Planned stuff - Tabs -- Being able to choose the css on export to pdf or markdown -- More tools in the toolbar +- Being able to choose the css on export to pdf or markdown +- More tools in the toolbar # Screenshots *ℹ Keep in mind there is more stuff than shown on the screenshots.* @@ -30,7 +30,7 @@ ![light mode loaded file](https://raw.githubusercontent.com/LukeOnuke/lmark/main/images/light-open.png) # Contributions -If you want to contribute to lmark, please do. Contributers are welcome. For build instructions go to [# Builds](#Builds) +If you want to contribute to lmark, please do. Contributers are welcome. For build instructions go to [# Builds](#builds) # Builds You can find the newest builds in the [package all platforms action](https://github.com/LukeOnuke/lmark/actions/workflows/main.yml), although be ware they might not work as intended. @@ -40,22 +40,22 @@ Preffered IDE is intelij IDEA but you could make it work on others ## Building the code yourself 1. **Git clone the repository** - ``` - git clone https://github.com/LukeOnuke/lmark.git - ``` + ``` + git clone https://github.com/LukeOnuke/lmark.git + ``` 2. **Navigate to project root** 3. **Package with maven** - ``` - mvn package - ``` + ``` + mvn package + ``` 4. *(Optional)* **Compile to native application** - ``` - jpackage --type app-image --name lmark-app-image / - --input C:\Users\lukak\Documents\GitHub\mdedit\target / - --main-jar lmark.jar --main-class com.lukeonuke.lmark.LMark / - --verbose --dest jpkg - ``` + ``` + jpackage --type app-image --name lmark-app-image / + --input C:\Users\lukak\Documents\GitHub\mdedit\target / + --main-jar lmark.jar --main-class com.lukeonuke.lmark.LMark / + --verbose --dest jpkg + ``` # Licence & legal This program uses [fonts awsome 6](https://fontawesome.com/) diff --git a/src/main/java/com/lukeonuke/lmark/gui/MainAppWindow.java b/src/main/java/com/lukeonuke/lmark/gui/MainAppWindow.java index 7177734..9c09f91 100644 --- a/src/main/java/com/lukeonuke/lmark/gui/MainAppWindow.java +++ b/src/main/java/com/lukeonuke/lmark/gui/MainAppWindow.java @@ -155,17 +155,7 @@ public void show() { final Menu openRecent = new Menu("Open recent"); fileMenu.getItems().add(openRecent); - try { - ArrayList recentsList = fileUtils.getRecentFiles(); - - recentsList.forEach(s -> { - MenuItem menuItem = new MenuItem(s); - menuItem.setOnAction(actionEvent -> fileUtils.setFile(new File(s))); - openRecent.getItems().add(menuItem); - }); - } catch (IOException ioex) { - fileMenu.getItems().remove(openRecent); - } + addRecentToMenu(fileMenu, openRecent); MenuItem saveFile = new MenuItem("Save"); saveFile.setOnAction(actionEvent -> fileUtils.saveFile(fileUtils.getFile(), edit.getText())); @@ -393,9 +383,12 @@ public void show() { * ============================ * */ fileUtils.registerFileListener(fileChangeEvent -> { + if(fileChangeEvent.getOldValue() != null && autosaveEnabled) save(edit.getText(), (File) fileChangeEvent.getOldValue()); readFileAndSet(edit, markdown); hoveredLink.setText(""); statusBar.getChildren().remove(hoveredLink); + + addRecentToMenu(fileMenu, openRecent); }); fileUtils.setFile(fileUtils.getFile()); readFileAndSet(edit, markdown); @@ -533,15 +526,19 @@ private synchronized static void setIsWorking(boolean bool) { } } - private void save(String text) { + private void save(String text, File file) { setIsWorking(true); - fileUtils.saveFile(fileUtils.getFile(), text); + fileUtils.saveFile(file, text); tampered = false; updateTitle(); logger.info("Saved hash = " + text.hashCode()); setIsWorking(false); } + private void save(String text){ + save(text, fileUtils.getFile()); + } + private void updateTitle() { stage.setTitle(ApplicationConstants.MAIN_WINDOW_TITLE + " - " + fileUtils.getFile().getPath()); if (autosaveEnabled) { @@ -673,7 +670,6 @@ private int getBeginningOfLine(TextArea textArea) { } private int getEndOfLine(TextArea textArea) { - logger.info(getBeginningOfLine(textArea) + " " + textArea.getLength() + " " + textArea.getText(getBeginningOfLine(textArea), textArea.getLength())); String text = textArea.getText(getBeginningOfLine(textArea), textArea.getLength()); if (!text.contains("\n")) return textArea.getLength(); return text.indexOf('\n') + getBeginningOfLine(textArea); @@ -794,6 +790,8 @@ private void print(Markdown markdown) { if (job.printDialog()) { job.print(); } + + document.close(); } catch (PrinterAbortException printerAbortException) { FxUtils.lazyRunOnPlatform(() -> { FxUtils.createAlert(Alert.AlertType.ERROR, "Printing error", @@ -812,4 +810,19 @@ private void print(Markdown markdown) { }, "print-worker"); t.start(); } + + private void addRecentToMenu(Menu fileMenu, Menu openRecent){ + try { + ArrayList recentsList = FileUtils.getRecentFiles(); + openRecent.getItems().clear(); + recentsList.forEach(s -> { + MenuItem menuItem = new MenuItem(s); + menuItem.setOnAction(actionEvent -> fileUtils.setFile(new File(s))); + openRecent.getItems().add(menuItem); + }); + } catch (IOException ioex) { + logger.info("Couldn't load (file > recent)", ioex.getCause()); + fileMenu.getItems().remove(openRecent); + } + } } diff --git a/src/main/java/com/lukeonuke/lmark/gui/StartWindow.java b/src/main/java/com/lukeonuke/lmark/gui/StartWindow.java index 61b3eb4..fbed6f9 100644 --- a/src/main/java/com/lukeonuke/lmark/gui/StartWindow.java +++ b/src/main/java/com/lukeonuke/lmark/gui/StartWindow.java @@ -53,22 +53,9 @@ public void show() { root.getStyleClass().add("gradient"); - ArrayList recentFilesList = new ArrayList<>(); - File recentFilesStorage = FileUtils.getRelativeFile(ApplicationConstants.RECENT_FILES_STORAGE); + ArrayList recentFilesList; try { - if (!recentFilesStorage.exists()) { - - recentFilesStorage.createNewFile(); - FileUtils.writeJSON(recentFilesList, recentFilesStorage); - - } - recentFilesList = FileUtils.readJSON(recentFilesStorage.getPath(), - new TypeToken>() { - }.getType()); - - if(recentFilesList == null){ - recentFilesList = new ArrayList<>(); - } + recentFilesList = FileUtils.getRecentFiles(); recentFilesList.forEach(s -> { recentFiles.getItems().add(new FileCell(new File(s))); diff --git a/src/main/java/com/lukeonuke/lmark/util/FileUtils.java b/src/main/java/com/lukeonuke/lmark/util/FileUtils.java index 947447c..c5b8bb7 100644 --- a/src/main/java/com/lukeonuke/lmark/util/FileUtils.java +++ b/src/main/java/com/lukeonuke/lmark/util/FileUtils.java @@ -223,13 +223,7 @@ public static void addToRecents(File recentFile) { File recentFilesStorage = FileUtils.getRelativeFile(ApplicationConstants.RECENT_FILES_STORAGE); ArrayList recent; try { - recent = FileUtils.readJSON(recentFilesStorage.getPath(), - new TypeToken>() { - }.getType()); - - if (recent == null) { - recent = new ArrayList<>(); - } + recent = FileUtils.getRecentFiles(); if (recent.contains(recentFile.getAbsolutePath())) { recent.remove(recentFile.getAbsolutePath()); @@ -250,7 +244,7 @@ public static void addToRecents(File recentFile) { } } - public ArrayList getRecentFiles() throws IOException { + public static ArrayList getRecentFiles() throws IOException { ArrayList recentFilesList = new ArrayList<>(); File recentFilesStorage = FileUtils.getRelativeFile(ApplicationConstants.RECENT_FILES_STORAGE); @@ -267,6 +261,18 @@ public ArrayList getRecentFiles() throws IOException { if (recentFilesList == null) { recentFilesList = new ArrayList<>(); } + + + File recent; + String s; + for (int i = 0; i < recentFilesList.size(); i++) { + s = recentFilesList.get(i); + recent = new File(s); + if (!recent.exists()) { + recentFilesList.remove(s); + } + } + return recentFilesList; }