Skip to content

Commit

Permalink
Fix passage suggest to allow spaces. Closes #1176
Browse files Browse the repository at this point in the history
  • Loading branch information
hituro committed Jan 31, 2025
1 parent e8c9588 commit 71af894
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
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

0 comments on commit 71af894

Please sign in to comment.