Gitea CI / Build Android APK (pull_request) Canceled after 0s
Gitea CI / Build Windows x64 app (pull_request) Canceled after 0s
Gitea CI / Audit and package Windows x64 (pull_request) Canceled after 0s
Gitea CI / Assemble and validate CI artifacts (pull_request) Canceled after 0s
Gitea CI / Gitea CI passed (pull_request) Canceled after 0s
Gitea CI / Check code and tests (pull_request) Canceled after 6m29s
153 lines
4.6 KiB
Dart
153 lines
4.6 KiB
Dart
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
import 'package:cutdex_sdk/project.dart';
|
|
import 'package:cutdex_ui/icons.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../l10n/app_localizations.dart';
|
|
import 'composer_document.dart';
|
|
|
|
part 'composer_suggestions.freezed.dart';
|
|
|
|
enum ComposerOptionGroup { media, timeline }
|
|
|
|
@freezed
|
|
sealed class ComposerOption with _$ComposerOption {
|
|
const factory ComposerOption.reference({
|
|
required String trigger,
|
|
required String title,
|
|
required String subtitle,
|
|
required IconData icon,
|
|
ComposerOptionGroup? group,
|
|
required ComposerReference reference,
|
|
}) = ComposerReferenceOption;
|
|
const factory ComposerOption.text({
|
|
required String trigger,
|
|
required String title,
|
|
required String subtitle,
|
|
required IconData icon,
|
|
ComposerOptionGroup? group,
|
|
required String text,
|
|
}) = ComposerTextOption;
|
|
const factory ComposerOption.invoke({
|
|
required String trigger,
|
|
required String title,
|
|
required String subtitle,
|
|
required IconData icon,
|
|
ComposerOptionGroup? group,
|
|
required VoidCallback onInvoke,
|
|
}) = ComposerInvokeOption;
|
|
const factory ComposerOption.openPicker({
|
|
required String trigger,
|
|
required String title,
|
|
required String subtitle,
|
|
required IconData icon,
|
|
ComposerOptionGroup? group,
|
|
required VoidCallback onInvoke,
|
|
}) = ComposerPickerOption;
|
|
}
|
|
|
|
/// System-owned entries. More triggers and actions register here later.
|
|
abstract final class ComposerOptionCatalog {
|
|
static List<ComposerOption> forTrigger(
|
|
String trigger, {
|
|
required AppLocalizations l10n,
|
|
required String? projectId,
|
|
required Iterable<AssetDocument> assets,
|
|
required Iterable<TimelineSummary> timelines,
|
|
}) {
|
|
if (trigger != '@' || projectId == null) return const [];
|
|
return [
|
|
for (final asset in assets)
|
|
ComposerOption.reference(
|
|
trigger: '@',
|
|
title: asset.name,
|
|
subtitle: l10n.materials,
|
|
icon: switch (asset.metadata['kind']) {
|
|
'video' => AppIcons.video,
|
|
'audio' => AppIcons.audio,
|
|
'image' => AppIcons.image,
|
|
_ => AppIcons.file,
|
|
},
|
|
group: ComposerOptionGroup.media,
|
|
reference: ComposerReference(
|
|
kind: ComposerReferenceKind.media,
|
|
projectId: projectId,
|
|
id: asset.id,
|
|
label: asset.name,
|
|
),
|
|
),
|
|
for (final timeline in timelines)
|
|
ComposerOption.reference(
|
|
trigger: '@',
|
|
title: timeline.name,
|
|
subtitle: l10n.timelines,
|
|
icon: AppIcons.timeline,
|
|
group: ComposerOptionGroup.timeline,
|
|
reference: ComposerReference(
|
|
kind: ComposerReferenceKind.timeline,
|
|
projectId: projectId,
|
|
id: timeline.id,
|
|
label: timeline.name,
|
|
),
|
|
),
|
|
];
|
|
}
|
|
}
|
|
|
|
@freezed
|
|
abstract class ComposerTriggerQuery with _$ComposerTriggerQuery {
|
|
const factory ComposerTriggerQuery({
|
|
required String trigger,
|
|
required int start,
|
|
required int end,
|
|
required String query,
|
|
}) = _ComposerTriggerQuery;
|
|
}
|
|
|
|
ComposerTriggerQuery? findComposerTrigger(
|
|
TextEditingValue value,
|
|
Iterable<ComposerMark> marks,
|
|
) {
|
|
if (!value.selection.isValid || !value.selection.isCollapsed) return null;
|
|
if (value.composing.isValid && !value.composing.isCollapsed) return null;
|
|
final caret = value.selection.extentOffset;
|
|
if (caret <= 0 || caret > value.text.length) return null;
|
|
for (final mark in marks) {
|
|
if (caret > mark.start && caret <= mark.end) return null;
|
|
}
|
|
final lineStart = value.text.lastIndexOf('\n', caret - 1) + 1;
|
|
for (var i = caret - 1; i >= lineStart; i--) {
|
|
final char = value.text[i];
|
|
if (char != '@' && char != '/' && char != r'$') continue;
|
|
if (i > 0 &&
|
|
!RegExp(r'[\s(\[{,;:!?,。!?;:、(“\u3400-\u9fff]')
|
|
.hasMatch(value.text[i - 1])) {
|
|
continue;
|
|
}
|
|
if (caret - i > 128) return null;
|
|
if (marks.any((mark) => i >= mark.start && i < mark.end)) return null;
|
|
final query = value.text.substring(i + 1, caret);
|
|
if (char == '/' && (query.contains('/') || query.contains('\\'))) {
|
|
return null;
|
|
}
|
|
return ComposerTriggerQuery(
|
|
trigger: char,
|
|
start: i,
|
|
end: caret,
|
|
query: query,
|
|
);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
List<ComposerOption> filterComposerOptions(
|
|
Iterable<ComposerOption> options,
|
|
String query,
|
|
) {
|
|
final normalized = query.trim().toLowerCase();
|
|
if (normalized.isEmpty) return options.toList();
|
|
return options
|
|
.where((option) => option.title.toLowerCase().contains(normalized))
|
|
.toList();
|
|
}
|