Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix passage suggest to allow spaces. Closes #1176 #1581

Merged
merged 1 commit into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/components/control/code-area/code-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import 'codemirror/addon/display/placeholder';
import 'codemirror/addon/hint/show-hint';
import 'codemirror/addon/hint/show-hint.css';
import 'codemirror/addon/search/searchcursor';
import 'codemirror/lib/codemirror.css';
import 'codemirror/mode/css/css';
import 'codemirror/mode/javascript/javascript';
Expand Down
12 changes: 10 additions & 2 deletions src/store/__tests__/use-codemirror-passage-hints.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ describe('useCodeMirrorPassageHints()', () => {
findWordAt: () => ({anchor: 0, head: 0}),
getCursor: jest.fn(),
getRange: () => 'a',
showHint: jest.fn()
showHint: jest.fn(),
getDoc: () => ({ getSearchCursor: () => ({
findPrevious: jest.fn(),
from: () => ({ch: 0})
})})
};
const story = fakeStory(3);

Expand All @@ -32,7 +36,11 @@ describe('useCodeMirrorPassageHints()', () => {
findWordAt: () => ({anchor: 0, head: 0}),
getCursor: jest.fn(),
getRange: () => 'a',
showHint: jest.fn()
showHint: jest.fn(),
getDoc: () => ({ getSearchCursor: () => ({
findPrevious: jest.fn(),
from: () => ({ch: 0})
})})
};
const story = fakeStory(3);

Expand Down
37 changes: 13 additions & 24 deletions src/store/use-codemirror-passage-hints.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
import CodeMirror, {Editor, Handle} from 'codemirror';
import CodeMirror, {Editor} from 'codemirror';
import * as React from 'react';
import {Story} from './stories';

type HintExtraKeyHandler = (editor: Editor, hint: Handle) => void;

export function useCodeMirrorPassageHints(story: Story) {
return React.useCallback(
(editor: Editor) => {
editor.showHint({
completeSingle: false,
extraKeys:
// Any of the characters below are an indication the user is finished
// typing a passage name in a link and the hint should close. We need
// to 'type' this character for the user since our handler consumes
// the event.
[']', '-', '|'].reduce<Record<string, HintExtraKeyHandler>>(
(result, character) => {
result[character] = (editor, hint) => {
const doc = editor.getDoc();

doc.replaceRange(character, doc.getCursor());
hint.close();
};

return result;
},
{}
),
closeCharacters: /\]/,
hint() {
const wordRange = editor.findWordAt(editor.getCursor());

const doc = editor.getDoc();
const cursor = editor.getCursor();
const wordRange = editor.findWordAt(cursor);
const linkCursor = doc.getSearchCursor(/\[\[|->|\|/,cursor);
const linkMatch = linkCursor.findPrevious();
const linkDelim = Array.isArray(linkMatch) ? linkMatch[0] : '';
const linkStart = linkCursor.from();
linkStart.ch += linkDelim.length;
const word = editor
.getRange(wordRange.anchor, wordRange.head)
.getRange(linkStart, cursor)
.toLowerCase();

const comps = {
Expand All @@ -41,7 +30,7 @@ export function useCodeMirrorPassageHints(story: Story) {

return result;
}, []),
from: wordRange.anchor,
from: linkStart,
to: wordRange.head
};

Expand Down
Loading