initial support for .org files

This commit is contained in:
Alen Šiljak
2021-03-14 01:33:59 +01:00
committed by Vishesh Handa
parent 1b6c76abb2
commit 171126aa87
4 changed files with 64 additions and 8 deletions

View File

@ -54,16 +54,15 @@ enum NoteType {
class NoteFileFormatInfo { class NoteFileFormatInfo {
static List<String> allowedExtensions() { static List<String> allowedExtensions() {
return [ return ['.md', '.org', '.txt'];
'.md',
'.txt',
];
} }
static String defaultExtension(NoteFileFormat format) { static String defaultExtension(NoteFileFormat format) {
switch (format) { switch (format) {
case NoteFileFormat.Markdown: case NoteFileFormat.Markdown:
return ".md"; return ".md";
case NoteFileFormat.OrgMode:
return '.org';
case NoteFileFormat.Txt: case NoteFileFormat.Txt:
return ".txt"; return ".txt";
default: default:
@ -75,13 +74,15 @@ class NoteFileFormatInfo {
var noteFilePath = filePath.toLowerCase(); var noteFilePath = filePath.toLowerCase();
var isMarkdownFile = noteFilePath.endsWith('.md'); var isMarkdownFile = noteFilePath.endsWith('.md');
var isTxtFile = noteFilePath.endsWith('.txt'); var isTxtFile = noteFilePath.endsWith('.txt');
var isOrgFile = noteFilePath.endsWith('.org');
return isMarkdownFile || isTxtFile; return isMarkdownFile || isTxtFile || isOrgFile;
} }
} }
enum NoteFileFormat { enum NoteFileFormat {
Markdown, Markdown,
OrgMode,
Txt, Txt,
} }
@ -159,11 +160,18 @@ class Note with NotesNotifier {
_filePath = p.join(parent.folderPath, Uuid().v4()); _filePath = p.join(parent.folderPath, Uuid().v4());
} }
switch (_fileFormat) { switch (_fileFormat) {
case NoteFileFormat.OrgMode:
if (!_filePath.toLowerCase().endsWith('.org')) {
_filePath += '.org';
}
break;
case NoteFileFormat.Txt: case NoteFileFormat.Txt:
if (!_filePath.toLowerCase().endsWith('.txt')) { if (!_filePath.toLowerCase().endsWith('.txt')) {
_filePath += '.txt'; _filePath += '.txt';
} }
break; break;
case NoteFileFormat.Markdown: case NoteFileFormat.Markdown:
default: default:
if (!_filePath.toLowerCase().endsWith('.md')) { if (!_filePath.toLowerCase().endsWith('.md')) {
@ -288,7 +296,8 @@ class Note with NotesNotifier {
} }
bool get canHaveMetadata { bool get canHaveMetadata {
if (_fileFormat == NoteFileFormat.Txt) { if (_fileFormat == NoteFileFormat.Txt ||
_fileFormat == NoteFileFormat.OrgMode) {
return false; return false;
} }
return parent.config.yamlHeaderEnabled; return parent.config.yamlHeaderEnabled;
@ -356,6 +365,7 @@ class Note with NotesNotifier {
var fpLowerCase = _filePath.toLowerCase(); var fpLowerCase = _filePath.toLowerCase();
var isMarkdown = fpLowerCase.endsWith('.md'); var isMarkdown = fpLowerCase.endsWith('.md');
var isTxt = fpLowerCase.endsWith('.txt'); var isTxt = fpLowerCase.endsWith('.txt');
var isOrg = fpLowerCase.endsWith('.org');
if (isMarkdown) { if (isMarkdown) {
try { try {
@ -379,6 +389,17 @@ class Note with NotesNotifier {
} catch (e, stackTrace) { } catch (e, stackTrace) {
logExceptionWarning(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; _loadState = NoteLoadState.Error;
_notifyModified(); _notifyModified();
return _loadState; return _loadState;
@ -419,9 +440,17 @@ class Note with NotesNotifier {
await file.delete(); await file.delete();
} }
///
/// Do not let the user rename it to a different file-type.
///
void rename(String newName) { void rename(String newName) {
// Do not let the user rename it to a non-markdown file
switch (_fileFormat) { switch (_fileFormat) {
case NoteFileFormat.OrgMode:
if (!newName.toLowerCase().endsWith('.org')) {
newName += '.org';
}
break;
case NoteFileFormat.Txt: case NoteFileFormat.Txt:
if (!newName.toLowerCase().endsWith('.txt')) { if (!newName.toLowerCase().endsWith('.txt')) {
newName += '.txt'; newName += '.txt';

View File

@ -144,6 +144,12 @@ class NoteEditorState extends State<NoteEditor> with WidgetsBindingObserver {
} }
} }
// Org files
if (note.fileFormat == NoteFileFormat.OrgMode &&
editorType == EditorType.Markdown) {
editorType = EditorType.Raw;
}
// Txt files // Txt files
if (note.fileFormat == NoteFileFormat.Txt && if (note.fileFormat == NoteFileFormat.Txt &&
editorType == EditorType.Markdown) { editorType == EditorType.Markdown) {

View File

@ -68,6 +68,7 @@ class LinkResolver {
var lowerCaseTerm = term.toLowerCase(); var lowerCaseTerm = term.toLowerCase();
var termEndsWithMd = lowerCaseTerm.endsWith('.md'); var termEndsWithMd = lowerCaseTerm.endsWith('.md');
var termEndsWithTxt = lowerCaseTerm.endsWith('.txt'); var termEndsWithTxt = lowerCaseTerm.endsWith('.txt');
var termEndsWithOrg = lowerCaseTerm.endsWith('.org');
var rootFolder = inputNote.parent.rootFolder; var rootFolder = inputNote.parent.rootFolder;
for (var note in rootFolder.getAllNotes()) { for (var note in rootFolder.getAllNotes()) {
@ -85,6 +86,19 @@ class LinkResolver {
if (f == term) { if (f == term) {
return note; 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')) { } else if (fileName.toLowerCase().endsWith('.txt')) {
if (termEndsWithTxt) { if (termEndsWithTxt) {
if (fileName == term) { 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')) { if (!spec.endsWith('.txt')) {
linkedNote = folder.getNoteWithSpec(spec + '.txt'); linkedNote = folder.getNoteWithSpec(spec + '.txt');
if (linkedNote != null) { if (linkedNote != null) {

View File

@ -32,7 +32,7 @@ class NoteEditorSelector extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
var list = Column( var list = Column(
children: <Widget>[ children: <Widget>[
if (fileFormat != NoteFileFormat.Txt) if (fileFormat == NoteFileFormat.Markdown)
_buildTile( _buildTile(
context, context,
EditorType.Markdown, EditorType.Markdown,