Skip to content
Closed
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
54 changes: 28 additions & 26 deletions super_editor/lib/src/clones/slack/slack_tags.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<EditRequestHandler> customRequestHandlers = const [],
this.trigger = "@",
}) : tagIndex = SlackTagIndex() {
_requestHandlers = <EditRequestHandler>[
(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;

Expand Down Expand Up @@ -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
Expand All @@ -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!;
Expand Down Expand Up @@ -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),
Expand All @@ -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.
Expand Down Expand Up @@ -240,7 +245,7 @@ class CancelComposingSlackTagCommand implements EditCommand {
final document = context.find<MutableDocument>(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!;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -788,7 +793,7 @@ class SlackTagIndex with ChangeNotifier implements Editable {
ValueListenable<ComposingSlackTag?> get composingSlackTag => _composingSlackTag;
final _composingSlackTag = ValueNotifier<ComposingSlackTag?>(null);

void _onComposingTagFound(ComposingSlackTag? tag) {
void setTag(ComposingSlackTag? tag) {
_composingSlackTag.value = tag;
}

Expand Down Expand Up @@ -969,7 +974,7 @@ class AdjustSelectionAroundSlackTagReaction implements EditReaction {
);
}

print(
editorSlackTagsLog.fine(
"Selection after adjusting for tag: ${editContext.find<MutableDocumentComposer>(Editor.composerKey).selection?.extent.nodePosition}");
}

Expand Down Expand Up @@ -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.");
}
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -1259,7 +1261,7 @@ class AdjustSelectionAroundSlackTagReaction implements EditReaction {
),
);

print("Setting selection to: $newSelection");
editorSlackTagsLog.fine("Setting selection to: $newSelection");

requestDispatcher.execute([
ChangeSelectionRequest(
Expand Down