From 171126aa87146e1ef72c2e1a4194ea74c91b0e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alen=20=C5=A0iljak?= Date: Sun, 14 Mar 2021 01:33:59 +0100 Subject: [PATCH] initial support for .org files --- lib/core/note.dart | 43 ++++++++++++++++++++++----- lib/screens/note_editor.dart | 6 ++++ lib/utils/link_resolver.dart | 21 +++++++++++++ lib/widgets/note_editor_selector.dart | 2 +- 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/lib/core/note.dart b/lib/core/note.dart index 1a37009c..7fae18ac 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -54,16 +54,15 @@ enum NoteType { class NoteFileFormatInfo { static List allowedExtensions() { - return [ - '.md', - '.txt', - ]; + return ['.md', '.org', '.txt']; } static String defaultExtension(NoteFileFormat format) { switch (format) { case NoteFileFormat.Markdown: return ".md"; + case NoteFileFormat.OrgMode: + return '.org'; case NoteFileFormat.Txt: return ".txt"; default: @@ -75,13 +74,15 @@ class NoteFileFormatInfo { var noteFilePath = filePath.toLowerCase(); var isMarkdownFile = noteFilePath.endsWith('.md'); var isTxtFile = noteFilePath.endsWith('.txt'); + var isOrgFile = noteFilePath.endsWith('.org'); - return isMarkdownFile || isTxtFile; + return isMarkdownFile || isTxtFile || isOrgFile; } } enum NoteFileFormat { Markdown, + OrgMode, Txt, } @@ -159,11 +160,18 @@ class Note with NotesNotifier { _filePath = p.join(parent.folderPath, Uuid().v4()); } switch (_fileFormat) { + case NoteFileFormat.OrgMode: + if (!_filePath.toLowerCase().endsWith('.org')) { + _filePath += '.org'; + } + break; + case NoteFileFormat.Txt: if (!_filePath.toLowerCase().endsWith('.txt')) { _filePath += '.txt'; } break; + case NoteFileFormat.Markdown: default: if (!_filePath.toLowerCase().endsWith('.md')) { @@ -288,7 +296,8 @@ class Note with NotesNotifier { } bool get canHaveMetadata { - if (_fileFormat == NoteFileFormat.Txt) { + if (_fileFormat == NoteFileFormat.Txt || + _fileFormat == NoteFileFormat.OrgMode) { return false; } return parent.config.yamlHeaderEnabled; @@ -356,6 +365,7 @@ class Note with NotesNotifier { var fpLowerCase = _filePath.toLowerCase(); var isMarkdown = fpLowerCase.endsWith('.md'); var isTxt = fpLowerCase.endsWith('.txt'); + var isOrg = fpLowerCase.endsWith('.org'); if (isMarkdown) { try { @@ -379,6 +389,17 @@ class Note with NotesNotifier { } catch (e, stackTrace) { logExceptionWarning(e, stackTrace); + _loadState = NoteLoadState.Error; + _notifyModified(); + return _loadState; + } + } else if (isOrg) { + try { + body = await File(_filePath).readAsString(); + _fileFormat = NoteFileFormat.OrgMode; + } catch (e, stackTrace) { + logExceptionWarning(e, stackTrace); + _loadState = NoteLoadState.Error; _notifyModified(); return _loadState; @@ -419,9 +440,17 @@ class Note with NotesNotifier { await file.delete(); } + /// + /// Do not let the user rename it to a different file-type. + /// void rename(String newName) { - // Do not let the user rename it to a non-markdown file switch (_fileFormat) { + case NoteFileFormat.OrgMode: + if (!newName.toLowerCase().endsWith('.org')) { + newName += '.org'; + } + break; + case NoteFileFormat.Txt: if (!newName.toLowerCase().endsWith('.txt')) { newName += '.txt'; diff --git a/lib/screens/note_editor.dart b/lib/screens/note_editor.dart index 61e3e7e8..bb01ec42 100644 --- a/lib/screens/note_editor.dart +++ b/lib/screens/note_editor.dart @@ -144,6 +144,12 @@ class NoteEditorState extends State with WidgetsBindingObserver { } } + // Org files + if (note.fileFormat == NoteFileFormat.OrgMode && + editorType == EditorType.Markdown) { + editorType = EditorType.Raw; + } + // Txt files if (note.fileFormat == NoteFileFormat.Txt && editorType == EditorType.Markdown) { diff --git a/lib/utils/link_resolver.dart b/lib/utils/link_resolver.dart index fcc231b0..65e39d64 100644 --- a/lib/utils/link_resolver.dart +++ b/lib/utils/link_resolver.dart @@ -68,6 +68,7 @@ class LinkResolver { var lowerCaseTerm = term.toLowerCase(); var termEndsWithMd = lowerCaseTerm.endsWith('.md'); var termEndsWithTxt = lowerCaseTerm.endsWith('.txt'); + var termEndsWithOrg = lowerCaseTerm.endsWith('.org'); var rootFolder = inputNote.parent.rootFolder; for (var note in rootFolder.getAllNotes()) { @@ -85,6 +86,19 @@ class LinkResolver { if (f == term) { return note; } + } else if (fileName.toLowerCase().endsWith('.org')) { + if (termEndsWithOrg) { + if (fileName == term) { + return note; + } else { + continue; + } + } + + var f = fileName.substring(0, fileName.length - 4); + if (f == term) { + return note; + } } else if (fileName.toLowerCase().endsWith('.txt')) { if (termEndsWithTxt) { if (fileName == term) { @@ -124,6 +138,13 @@ class LinkResolver { } } + if (!spec.endsWith('.org')) { + linkedNote = folder.getNoteWithSpec(spec + '.org'); + if (linkedNote != null) { + return linkedNote; + } + } + if (!spec.endsWith('.txt')) { linkedNote = folder.getNoteWithSpec(spec + '.txt'); if (linkedNote != null) { diff --git a/lib/widgets/note_editor_selector.dart b/lib/widgets/note_editor_selector.dart index e232aa56..f2dc4f73 100644 --- a/lib/widgets/note_editor_selector.dart +++ b/lib/widgets/note_editor_selector.dart @@ -32,7 +32,7 @@ class NoteEditorSelector extends StatelessWidget { Widget build(BuildContext context) { var list = Column( children: [ - if (fileFormat != NoteFileFormat.Txt) + if (fileFormat == NoteFileFormat.Markdown) _buildTile( context, EditorType.Markdown,