mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 15:26:17 +00:00
feat(app): hubs, types and tags UI, themes, key bindings and slash commands
Settings screen (themes incl. Sweet, rebindable shortcuts), rich text escapes, page sidebar with the local graph, sortable All pages, tree view changes and the Type/Tags rows in the page header. Docs updated. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e18fd2c27e
commit
88ef4a97a5
32 files changed
+5431
-375
No files matched your search
@@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
|
||||
|
||||
import '../backend/graph_backend.dart';
|
||||
import '../state/app_state.dart';
|
||||
import '../state/keymap.dart';
|
||||
import '../state/outline_controller.dart';
|
||||
import '../state/outline_ops.dart';
|
||||
import '../theme/theme.dart';
|
||||
@@ -512,6 +513,12 @@ class _BlockEditorState extends State<BlockEditor> {
|
||||
final seq = ++_searchSeq;
|
||||
final backend = c.backend;
|
||||
Future<List<CompletionItem>> search() async {
|
||||
if (q.kind == CompletionKind.command) {
|
||||
return [
|
||||
for (final cmd in matchCommands(q.query))
|
||||
CompletionItem('/${cmd.name}', cmd.name, detail: cmd.label),
|
||||
];
|
||||
}
|
||||
if (q.kind == CompletionKind.block) {
|
||||
final hits = await backend.searchBlocks(q.query, limit: 8);
|
||||
return [
|
||||
@@ -564,10 +571,35 @@ class _BlockEditorState extends State<BlockEditor> {
|
||||
final q = _query;
|
||||
if (q == null || i >= _items.length) return;
|
||||
final v = applyCompletion(_text.text, q, _items[i]);
|
||||
final again =
|
||||
q.kind == CompletionKind.command &&
|
||||
slashCommands.firstWhere((c) => c.name == _items[i].value).then;
|
||||
_closeCompletion();
|
||||
_text.value = v;
|
||||
_last = v;
|
||||
c.setText(n, v.text);
|
||||
// `/link` leaves the caret inside `[[]]`: go straight on to the page picker.
|
||||
if (again) _updateCompletion();
|
||||
}
|
||||
|
||||
/// Applies an editor formatting shortcut to the selection; false if `a` is not one.
|
||||
bool _format(AppAction a) {
|
||||
final (open, close) = switch (a) {
|
||||
AppAction.bold => ('**', '**'),
|
||||
AppAction.italic => ('*', '*'),
|
||||
AppAction.strike => ('~~', '~~'),
|
||||
AppAction.highlight => ('==', '=='),
|
||||
AppAction.code => ('`', '`'),
|
||||
AppAction.link => ('[[', ']]'),
|
||||
_ => ('', ''),
|
||||
};
|
||||
if (open.isEmpty) return false;
|
||||
final v = wrapSelection(_text.value, open, close);
|
||||
_text.value = v;
|
||||
_last = v;
|
||||
c.setText(n, v.text);
|
||||
_updateCompletion();
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------- keys
|
||||
@@ -619,6 +651,14 @@ class _BlockEditorState extends State<BlockEditor> {
|
||||
final caret = sel.isValid ? sel.extentOffset : _text.text.length;
|
||||
final len = _text.text.length;
|
||||
|
||||
if (ctrl || alt) {
|
||||
final bound = AppScope.read(context).keymap.actionFor(
|
||||
Combo(key, ctrl: ctrl, shift: shift, alt: alt),
|
||||
editor: true,
|
||||
);
|
||||
if (bound != null && _format(bound)) return KeyEventResult.handled;
|
||||
}
|
||||
|
||||
if (_completing) {
|
||||
if (key == LogicalKeyboardKey.arrowDown) {
|
||||
setState(() => _selected = (_selected + 1) % _items.length);
|
||||
@@ -842,6 +882,8 @@ class CompletionPopup extends StatelessWidget {
|
||||
? Icons.add_rounded
|
||||
: kind == CompletionKind.block
|
||||
? Icons.notes_rounded
|
||||
: kind == CompletionKind.command
|
||||
? Icons.bolt_rounded
|
||||
: kind == CompletionKind.tag
|
||||
? Icons.tag_rounded
|
||||
: Icons.description_outlined,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
enum CompletionKind { page, tag, block }
|
||||
enum CompletionKind { page, tag, block, command }
|
||||
|
||||
/// An autocomplete trigger under the caret: `[[query`, `#query` or `((query`.
|
||||
/// An autocomplete trigger under the caret: `[[query`, `#query`, `((query` or `/query`.
|
||||
class CompletionQuery {
|
||||
const CompletionQuery(this.kind, this.start, this.query);
|
||||
|
||||
@@ -39,6 +39,121 @@ class CompletionItem {
|
||||
|
||||
bool _ws(String c) => c.trim().isEmpty;
|
||||
|
||||
/// A `/command`. `insert` is the text it produces; `|` marks where the caret lands (end of
|
||||
/// the text when absent). `then` re-opens autocomplete afterwards (the link commands).
|
||||
class SlashCommand {
|
||||
const SlashCommand(
|
||||
this.name,
|
||||
this.label,
|
||||
this.insert, {
|
||||
this.keywords = '',
|
||||
this.then = false,
|
||||
});
|
||||
final String name;
|
||||
final String label;
|
||||
final String insert;
|
||||
final String keywords;
|
||||
final bool then;
|
||||
|
||||
/// Today's date as a journal title, for `/today`.
|
||||
static String today() {
|
||||
final d = DateTime.now();
|
||||
String two(int n) => n.toString().padLeft(2, '0');
|
||||
return '${d.year}-${two(d.month)}-${two(d.day)}';
|
||||
}
|
||||
|
||||
String text() => insert.replaceAll('{today}', today());
|
||||
}
|
||||
|
||||
const slashCommands = [
|
||||
SlashCommand(
|
||||
'link',
|
||||
'Link to a page',
|
||||
'[[|]]',
|
||||
keywords: 'page wiki',
|
||||
then: true,
|
||||
),
|
||||
SlashCommand(
|
||||
'ref',
|
||||
'Reference a block',
|
||||
'((|))',
|
||||
keywords: 'block',
|
||||
then: true,
|
||||
),
|
||||
SlashCommand('url', 'Web link', '[|](https://)', keywords: 'http markdown'),
|
||||
SlashCommand('image', 'Image', '', keywords: 'picture img'),
|
||||
SlashCommand('bold', 'Bold', '**|**', keywords: 'strong'),
|
||||
SlashCommand('italic', 'Italic', '*|*', keywords: 'emphasis'),
|
||||
SlashCommand('strike', 'Strikethrough', '~~|~~', keywords: 'delete'),
|
||||
SlashCommand('highlight', 'Highlight', '==|==', keywords: 'mark'),
|
||||
SlashCommand('code', 'Inline code', '`|`', keywords: 'mono'),
|
||||
SlashCommand('codeblock', 'Code block', '```\n|\n```', keywords: 'fence'),
|
||||
SlashCommand('h1', 'Heading 1', '# |', keywords: 'title'),
|
||||
SlashCommand('h2', 'Heading 2', '## |', keywords: 'title'),
|
||||
SlashCommand('h3', 'Heading 3', '### |', keywords: 'title'),
|
||||
SlashCommand('todo', 'Task', 'TODO |', keywords: 'task checkbox'),
|
||||
SlashCommand('quote', 'Quote', '> |', keywords: 'blockquote'),
|
||||
SlashCommand('divider', 'Divider', '---', keywords: 'rule line hr'),
|
||||
SlashCommand(
|
||||
'today',
|
||||
"Link to today's journal",
|
||||
'[[{today}]]',
|
||||
keywords: 'date',
|
||||
),
|
||||
SlashCommand('tag', 'Tag', '#|', keywords: 'hashtag', then: true),
|
||||
];
|
||||
|
||||
/// Commands matching `query` (name prefix first, then name/label/keyword substrings).
|
||||
List<SlashCommand> matchCommands(String query, {int limit = 8}) {
|
||||
final q = query.toLowerCase();
|
||||
final starts = <SlashCommand>[];
|
||||
final rest = <SlashCommand>[];
|
||||
for (final c in slashCommands) {
|
||||
if (c.name.startsWith(q)) {
|
||||
starts.add(c);
|
||||
} else if ('${c.label} ${c.keywords}'.toLowerCase().contains(q)) {
|
||||
rest.add(c);
|
||||
}
|
||||
}
|
||||
return [...starts, ...rest].take(limit).toList();
|
||||
}
|
||||
|
||||
/// Wraps the selection in `open`/`close` (or inserts the pair with the caret between). If the
|
||||
/// selection is already wrapped, unwraps it.
|
||||
TextEditingValue wrapSelection(TextEditingValue v, String open, String close) {
|
||||
final sel = v.selection;
|
||||
if (!sel.isValid) return v;
|
||||
final t = v.text;
|
||||
final inner = t.substring(sel.start, sel.end);
|
||||
if (!sel.isCollapsed &&
|
||||
sel.start >= open.length &&
|
||||
sel.end + close.length <= t.length &&
|
||||
t.substring(sel.start - open.length, sel.start) == open &&
|
||||
t.substring(sel.end, sel.end + close.length) == close) {
|
||||
return TextEditingValue(
|
||||
text:
|
||||
t.substring(0, sel.start - open.length) +
|
||||
inner +
|
||||
t.substring(sel.end + close.length),
|
||||
selection: TextSelection(
|
||||
baseOffset: sel.start - open.length,
|
||||
extentOffset: sel.end - open.length,
|
||||
),
|
||||
);
|
||||
}
|
||||
final text =
|
||||
t.substring(0, sel.start) + open + inner + close + t.substring(sel.end);
|
||||
return TextEditingValue(
|
||||
text: text,
|
||||
selection: sel.isCollapsed
|
||||
? TextSelection.collapsed(offset: sel.start + open.length)
|
||||
: TextSelection(
|
||||
baseOffset: sel.start + open.length,
|
||||
extentOffset: sel.end + open.length,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Finds the autocomplete trigger that the caret is in, if any.
|
||||
CompletionQuery? detectCompletion(String text, int caret) {
|
||||
if (caret < 0 || caret > text.length) return null;
|
||||
@@ -69,6 +184,15 @@ CompletionQuery? detectCompletion(String text, int caret) {
|
||||
),
|
||||
);
|
||||
}
|
||||
final slash = before.lastIndexOf('/');
|
||||
if (slash >= 0 && (slash == 0 || _ws(before[slash - 1]))) {
|
||||
final q = before.substring(slash + 1);
|
||||
if (!q.contains(RegExp(r'\s'))) {
|
||||
consider(
|
||||
CompletionQuery(CompletionKind.command, lineStart + slash + 1, q),
|
||||
);
|
||||
}
|
||||
}
|
||||
final hash = before.lastIndexOf('#');
|
||||
if (hash >= 0 && (hash == 0 || _ws(before[hash - 1]))) {
|
||||
final q = before.substring(hash + 1);
|
||||
@@ -98,6 +222,19 @@ TextEditingValue applyCompletion(
|
||||
final head = text.substring(0, q.start);
|
||||
var tail = text.substring(end);
|
||||
switch (q.kind) {
|
||||
case CompletionKind.command:
|
||||
final cmd = slashCommands.firstWhere((c) => c.name == item.value);
|
||||
final body = cmd.text();
|
||||
final at = body.indexOf('|');
|
||||
final clean = body.replaceFirst('|', '');
|
||||
// `head` still ends with the `/`.
|
||||
final before = head.substring(0, head.length - 1);
|
||||
return TextEditingValue(
|
||||
text: before + clean + tail,
|
||||
selection: TextSelection.collapsed(
|
||||
offset: before.length + (at < 0 ? clean.length : at),
|
||||
),
|
||||
);
|
||||
case CompletionKind.page:
|
||||
if (tail.startsWith(']]')) tail = tail.substring(2);
|
||||
final t = '$head${item.value}]]';
|
||||
|
||||
@@ -86,6 +86,9 @@ class BlockRefDtoLike {
|
||||
final String pageId;
|
||||
}
|
||||
|
||||
/// Characters a backslash can escape in a block (Markdown's ASCII punctuation).
|
||||
const escapable = r'\\`*_{}[]()#+-.!~=|<>:"';
|
||||
|
||||
final _marker = RegExp(r'^(TODO|DOING|NOW|LATER|DONE|CANCELED|CANCELLED) ');
|
||||
final _heading = RegExp(r'^(#{1,6}) ');
|
||||
final _uuid = RegExp(
|
||||
@@ -245,6 +248,13 @@ class BlockRenderer {
|
||||
|
||||
while (i < s.length) {
|
||||
final c = s[i];
|
||||
// Backslash escape: `\*` shows a literal `*` and hides the backslash.
|
||||
if (c == r'\' && i + 1 < s.length && escapable.contains(s[i + 1])) {
|
||||
flush(i);
|
||||
_text(s[i + 1], off + i + 1, style);
|
||||
i = plain = i + 2;
|
||||
continue;
|
||||
}
|
||||
// `code`
|
||||
if (c == '`') {
|
||||
final j = s.indexOf('`', i + 1);
|
||||
|
||||
Reference in new issue
Block a user