Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/frontend/lib/utils/document_builder.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:js_interop';

import 'package:flutter/material.dart';
import 'package:owlistic/core/theme.dart';
import 'package:owlistic/utils/data_converter.dart';
Expand Down Expand Up @@ -28,6 +30,7 @@ class DocumentBuilder {

// Plugins
late final ActionTagsPlugin _inlineCommandsPlugin;
late final PatternTagPlugin _inlineMetadataPlugin;

// Add instance of AttributedTextUtils
static final AttributedTextUtils _attributedTextUtils = AttributedTextUtils();
Expand Down Expand Up @@ -100,10 +103,18 @@ class DocumentBuilder {
createDefaultDocumentEditor(document: _document, composer: _composer);

// Add action tags listener for inline commands
_inlineCommandsPlugin = ActionTagsPlugin();
_inlineCommandsPlugin = ActionTagsPlugin(
tagRule: const TagRule(trigger: "/", excludedCharacters: {" "})
);
_inlineCommandsPlugin.attach(_editor);
_inlineCommandsPlugin.composingActionTag.addListener(_handleInlineCommand);

// Add pattern tags listener for inline metadata (due-date, assignee, etc)
_inlineMetadataPlugin = PatternTagPlugin(
tagRule: const TagRule(trigger: "!", excludedCharacters: {" "})
);
_inlineMetadataPlugin.attach(_editor);
_inlineMetadataPlugin.tagIndex.addListener(_handleInlineMetadata);

// Create focus node
_editorFocusNode = FocusNode();
Expand All @@ -116,6 +127,8 @@ class DocumentBuilder {
void dispose() {
_inlineCommandsPlugin.composingActionTag.removeListener(_handleInlineCommand);
_inlineCommandsPlugin.detach(_editor);
_inlineMetadataPlugin.tagIndex.removeListener(_handleInlineMetadata);
_inlineMetadataPlugin.detach(_editor);
_editorFocusNode.dispose();
_composer.dispose();
_editor.dispose();
Expand Down Expand Up @@ -190,6 +203,30 @@ class DocumentBuilder {
}
}

void _handleInlineMetadata() {
for (final node in _document) {
if (node is! TextNode) {
continue;
}

final tags = _inlineMetadataPlugin.tagIndex.getTagsInTextNode(node.id);

for (final tag in tags) {
final action = node.text.substring(tag.startOffset + 1, tag.endOffset);
final actionText = node.text.substring(tag.endOffset);
switch (action) {
case 'due':
_logger.info("Due Date: ${actionText}");
break;
case 'at':
case 'assign':
_logger.info("Assignee: ${actionText}");
break;
}
}
}
}

// Add document structure change listener to detect new/deleted nodes
void addDocumentStructureListener(void Function(dynamic) listener) {
_document.addListener(listener);
Expand Down Expand Up @@ -1363,8 +1400,10 @@ class DocumentBuilder {
MarkdownInlineUpstreamSyntaxPlugin(
parsers: const [ StyleUpstreamMarkdownSyntaxParser() ]
),
// Tasks
_actionTagPlugin,
// Inline Commands
_inlineCommandsPlugin,
// Inline Metadata
_inlineMetadataPlugin,
}),
)
);
Expand Down