Skip to content

Commit

Permalink
Fix autosave, recent files not updating, lot more
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeOnuke committed Nov 20, 2021
1 parent b8cbaf4 commit 79ce201
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 54 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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.
Expand All @@ -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/)
Expand Down
41 changes: 27 additions & 14 deletions src/main/java/com/lukeonuke/lmark/gui/MainAppWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,7 @@ public void show() {

final Menu openRecent = new Menu("Open recent");
fileMenu.getItems().add(openRecent);
try {
ArrayList<String> 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()));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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",
Expand All @@ -812,4 +810,19 @@ private void print(Markdown markdown) {
}, "print-worker");
t.start();
}

private void addRecentToMenu(Menu fileMenu, Menu openRecent){
try {
ArrayList<String> 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);
}
}
}
17 changes: 2 additions & 15 deletions src/main/java/com/lukeonuke/lmark/gui/StartWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,9 @@ public void show() {
root.getStyleClass().add("gradient");


ArrayList<String> recentFilesList = new ArrayList<>();
File recentFilesStorage = FileUtils.getRelativeFile(ApplicationConstants.RECENT_FILES_STORAGE);
ArrayList<String> recentFilesList;
try {
if (!recentFilesStorage.exists()) {

recentFilesStorage.createNewFile();
FileUtils.writeJSON(recentFilesList, recentFilesStorage);

}
recentFilesList = FileUtils.readJSON(recentFilesStorage.getPath(),
new TypeToken<ArrayList<String>>() {
}.getType());

if(recentFilesList == null){
recentFilesList = new ArrayList<>();
}
recentFilesList = FileUtils.getRecentFiles();

recentFilesList.forEach(s -> {
recentFiles.getItems().add(new FileCell(new File(s)));
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/com/lukeonuke/lmark/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,7 @@ public static void addToRecents(File recentFile) {
File recentFilesStorage = FileUtils.getRelativeFile(ApplicationConstants.RECENT_FILES_STORAGE);
ArrayList<String> recent;
try {
recent = FileUtils.readJSON(recentFilesStorage.getPath(),
new TypeToken<ArrayList<String>>() {
}.getType());

if (recent == null) {
recent = new ArrayList<>();
}
recent = FileUtils.getRecentFiles();

if (recent.contains(recentFile.getAbsolutePath())) {
recent.remove(recentFile.getAbsolutePath());
Expand All @@ -250,7 +244,7 @@ public static void addToRecents(File recentFile) {
}
}

public ArrayList<String> getRecentFiles() throws IOException {
public static ArrayList<String> getRecentFiles() throws IOException {
ArrayList<String> recentFilesList = new ArrayList<>();
File recentFilesStorage = FileUtils.getRelativeFile(ApplicationConstants.RECENT_FILES_STORAGE);

Expand All @@ -267,6 +261,18 @@ public ArrayList<String> 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;
}

Expand Down

0 comments on commit 79ce201

Please sign in to comment.