-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(supersearch): Insert space before qualifier (LWS-286) (#1198)
* fix(supersearch): Insert space before qualifier * build separate atomic rangeset * Handle space input before qualifier * Name file same as function
- Loading branch information
1 parent
0878806
commit 666663d
Showing
2 changed files
with
81 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/supersearch/src/lib/extensions/lxlQualifierPlugin/insertSpaceBeforeQualifier.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { EditorState, RangeSet, RangeValue } from '@codemirror/state'; | ||
|
||
const insertSpaceBeforeQualifier = (getRanges: () => RangeSet<RangeValue>) => { | ||
return EditorState.transactionFilter.of((tr) => { | ||
if (!tr.docChanged || (tr.isUserEvent('delete') && tr.state.selection.main.head === 0)) { | ||
return tr; | ||
} else { | ||
let insert = {}; | ||
let changes = {}; | ||
const atomicRanges = getRanges(); | ||
const oldCursorPos = tr.startState.selection.main.head; | ||
const newCursorPos = tr.state.selection.main.head; | ||
|
||
atomicRanges.between(oldCursorPos, oldCursorPos, (from) => { | ||
if (oldCursorPos === from) { | ||
// overlap is at atomic range start | ||
const input = !!tr.newDoc.slice(oldCursorPos, newCursorPos).toString().trim(); | ||
const isDelete = tr.isUserEvent('delete'); | ||
// don't add space if input is space | ||
// instead, move cursor back to old pos | ||
if (input || isDelete) { | ||
changes = { | ||
from: newCursorPos, | ||
to: newCursorPos, | ||
insert: ' ' | ||
}; | ||
} | ||
insert = { | ||
changes, | ||
sequential: true, | ||
selection: { anchor: input || isDelete ? newCursorPos : oldCursorPos } | ||
}; | ||
} | ||
return false; | ||
}); | ||
return [tr, insert]; | ||
} | ||
}); | ||
}; | ||
|
||
export default insertSpaceBeforeQualifier; |