diff --git a/super_editor/lib/src/clones/slack/slack_tags.dart b/super_editor/lib/src/clones/slack/slack_tags.dart index e3f466b545..70d445aace 100644 --- a/super_editor/lib/src/clones/slack/slack_tags.dart +++ b/super_editor/lib/src/clones/slack/slack_tags.dart @@ -31,26 +31,31 @@ class SlackTagPlugin extends SuperEditorPlugin { /// The key used to access the [SlackTagIndex] in an attached [Editor]. static const slackTagIndexKey = "slackTagIndex"; - static const _trigger = "@"; - - SlackTagPlugin() : tagIndex = SlackTagIndex() { + SlackTagPlugin({ + List customRequestHandlers = const [], + this.trigger = "@", + }) : tagIndex = SlackTagIndex() { _requestHandlers = [ (request) => - request is FillInComposingSlackTagRequest ? FillInComposingSlackTagCommand(_trigger, request.tag) : null, + request is FillInComposingSlackTagRequest ? FillInComposingSlackTagCommand(trigger, request.tag) : null, (request) => request is CancelComposingSlackTagRequest // ? const CancelComposingSlackTagCommand() : null, + ...customRequestHandlers, ]; _reactions = [ SlackTagReaction( - trigger: _trigger, - onUpdateComposingTag: tagIndex._onComposingTagFound, + trigger: trigger, + onUpdateComposingTag: tagIndex.setTag, ), - const AdjustSelectionAroundSlackTagReaction(_trigger), + AdjustSelectionAroundSlackTagReaction(trigger), ]; } + /// The character that triggers a tag. Default is "@". + final String trigger; + /// Index of all slack tags in the document, which changes as the user adds and removes tags. final SlackTagIndex tagIndex; @@ -128,11 +133,11 @@ class FillInComposingSlackTagRequest implements EditRequest { class FillInComposingSlackTagCommand implements EditCommand { const FillInComposingSlackTagCommand( - this._trigger, + this.trigger, this._tag, ); - final String _trigger; + final String trigger; final String _tag; @override @@ -145,7 +150,7 @@ class FillInComposingSlackTagCommand implements EditCommand { } // Remove the composing attribution from the text. - final removeComposingAttributionCommand = _removeSlackComposingTokenAttribution(document, tagIndex); + final removeComposingAttributionCommand = removeSlackComposingTokenAttribution(document, tagIndex); // Insert the final text and apply a stable tag attribution. final tag = tagIndex.composingSlackTag.value!; @@ -181,7 +186,7 @@ class FillInComposingSlackTagCommand implements EditCommand { InsertAttributedTextCommand( documentPosition: textNode.positionAt(startOfToken), textToInsert: AttributedText( - "$_trigger$_tag ", + "$trigger$_tag ", AttributedSpans( attributions: [ SpanMarker(attribution: slackTagAttribution, offset: 0, markerType: SpanMarkerType.start), @@ -207,8 +212,8 @@ class FillInComposingSlackTagCommand implements EditCommand { // FIXME: Use a transaction to bundle these changes so order doesn't matter. if (removeComposingAttributionCommand != null) { executor.executeCommand(removeComposingAttributionCommand); - print("Attributions immediately after removing composing attribution:"); - print("${textNode.text.getAttributionSpansByFilter((a) => true)}"); + editorSlackTagsLog.info("Attributions immediately after removing composing attribution:"); + editorSlackTagsLog.info("${textNode.text.getAttributionSpansByFilter((a) => true)}"); } // Reset the tag index so that we're no longer composing a tag. @@ -240,7 +245,7 @@ class CancelComposingSlackTagCommand implements EditCommand { final document = context.find(Editor.documentKey); // Remove the composing attribution from the text. - final removeComposingAttributionCommand = _removeSlackComposingTokenAttribution(document, tagIndex); + final removeComposingAttributionCommand = removeSlackComposingTokenAttribution(document, tagIndex); // Reset the tag index. final tag = tagIndex.composingSlackTag.value!; @@ -269,15 +274,15 @@ class CancelComposingSlackTagCommand implements EditCommand { } } -EditCommand? _removeSlackComposingTokenAttribution(Document document, SlackTagIndex tagIndex) { - print("REMOVING COMPOSING ATTRIBUTION"); +EditCommand? removeSlackComposingTokenAttribution(Document document, SlackTagIndex tagIndex) { + editorSlackTagsLog.info("REMOVING COMPOSING ATTRIBUTION"); // Remove any composing attribution for the previous state of the tag. // It's possible that the previous composing region disappeared, e.g., due to a deletion. final previousTag = tagIndex._composingSlackTag.value!; final previousTagNode = document.getNodeById(previousTag.contentBounds.start.nodeId); // We assume tags don't cross node boundaries. if (previousTagNode == null || previousTagNode is! TextNode) { - print("Couldn't find composing attribution. Fizzling."); + editorSlackTagsLog.info("Couldn't find composing attribution. Fizzling."); return null; } @@ -339,7 +344,7 @@ class SlackTagReaction implements EditReaction { editorSlackTagsLog.info("Is already composing a tag? ${tagIndex.isComposing}"); if (changeList.length == 1 && changeList.first is SelectionChangeEvent) { - print("Selection change event: ${(changeList.first as SelectionChangeEvent).changeType}"); + editorSlackTagsLog.info("Selection change event: ${(changeList.first as SelectionChangeEvent).changeType}"); } // Update the current tag composition. @@ -420,7 +425,7 @@ class SlackTagReaction implements EditReaction { onUpdateComposingTag?.call(newTag); // Add composing attribution for the updated tag bounds. - print("Updating composing attribution with bounds: ${newTag.contentBounds}"); + editorSlackTagsLog.info("Updating composing attribution with bounds: ${newTag.contentBounds}"); requestDispatcher.execute([ AddTextAttributionsRequest( documentRange: newTag.contentBounds, @@ -788,7 +793,7 @@ class SlackTagIndex with ChangeNotifier implements Editable { ValueListenable get composingSlackTag => _composingSlackTag; final _composingSlackTag = ValueNotifier(null); - void _onComposingTagFound(ComposingSlackTag? tag) { + void setTag(ComposingSlackTag? tag) { _composingSlackTag.value = tag; } @@ -969,7 +974,7 @@ class AdjustSelectionAroundSlackTagReaction implements EditReaction { ); } - print( + editorSlackTagsLog.fine( "Selection after adjusting for tag: ${editContext.find(Editor.composerKey).selection?.extent.nodePosition}"); } @@ -1097,10 +1102,7 @@ class AdjustSelectionAroundSlackTagReaction implements EditReaction { case SelectionChangeType.placeCaret: case SelectionChangeType.pushCaret: case SelectionChangeType.collapseSelection: - throw AssertionError( - "An expanded selection reported a SelectionChangeType for a collapsed selection: ${selectionChangeEvent.changeType}\n${selectionChangeEvent.newSelection}"); case SelectionChangeType.clearSelection: - throw AssertionError("Expected a collapsed selection but there was no selection."); } } @@ -1236,7 +1238,7 @@ class AdjustSelectionAroundSlackTagReaction implements EditReaction { case TextAffinity.downstream: // Move to ending edge. textOffset = tagAroundCaret.indexedTag.endOffset; - print("Pushing to text offset: $textOffset"); + editorSlackTagsLog.fine("Pushing to text offset: $textOffset"); break; } @@ -1259,7 +1261,7 @@ class AdjustSelectionAroundSlackTagReaction implements EditReaction { ), ); - print("Setting selection to: $newSelection"); + editorSlackTagsLog.fine("Setting selection to: $newSelection"); requestDispatcher.execute([ ChangeSelectionRequest(