mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
[cm6] Map active spellcheck range through doc changes (#14758)
GitOrigin-RevId: bc6a8f7d8c65edab0d3f2481a39b189a8f670508
This commit is contained in:
parent
73413f0b33
commit
c28c7d5da2
1 changed files with 26 additions and 21 deletions
|
@ -99,24 +99,33 @@ export const spellingMenuField = StateField.define<Tooltip | null>({
|
||||||
create() {
|
create() {
|
||||||
return null
|
return null
|
||||||
},
|
},
|
||||||
update(menu, transaction) {
|
update(value, transaction) {
|
||||||
|
if (value) {
|
||||||
|
value = {
|
||||||
|
...value,
|
||||||
|
pos: transaction.changes.mapPos(value.pos),
|
||||||
|
end: value.end ? transaction.changes.mapPos(value.end) : undefined,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (const effect of transaction.effects) {
|
for (const effect of transaction.effects) {
|
||||||
if (effect.is(hideSpellingMenu)) {
|
if (effect.is(hideSpellingMenu)) {
|
||||||
return null
|
value = null
|
||||||
} else if (effect.is(showSpellingMenu)) {
|
} else if (effect.is(showSpellingMenu)) {
|
||||||
const { mark, word } = effect.value
|
const { mark, word } = effect.value
|
||||||
// Build a "Tooltip" showing the suggestions
|
// Build a "Tooltip" showing the suggestions
|
||||||
return {
|
value = {
|
||||||
pos: mark.from,
|
pos: mark.from,
|
||||||
|
end: mark.to,
|
||||||
above: false,
|
above: false,
|
||||||
strictSide: false,
|
strictSide: false,
|
||||||
create: view => {
|
create: view => {
|
||||||
return createSpellingSuggestionList(mark, word, view)
|
return createSpellingSuggestionList(word, view)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return menu
|
return value
|
||||||
},
|
},
|
||||||
provide: field => {
|
provide: field => {
|
||||||
return [
|
return [
|
||||||
|
@ -143,11 +152,7 @@ const hideSpellingMenu = StateEffect.define()
|
||||||
* Creates the suggestion menu dom, to be displayed in the
|
* Creates the suggestion menu dom, to be displayed in the
|
||||||
* spelling menu "tooltip"
|
* spelling menu "tooltip"
|
||||||
* */
|
* */
|
||||||
const createSpellingSuggestionList = (
|
const createSpellingSuggestionList = (word: Word, view: EditorView) => {
|
||||||
mark: Mark,
|
|
||||||
word: Word,
|
|
||||||
view: EditorView
|
|
||||||
) => {
|
|
||||||
// Wrapper div.
|
// Wrapper div.
|
||||||
// Note, CM6 doesn't like showing complex elements
|
// Note, CM6 doesn't like showing complex elements
|
||||||
// 'inside' its Tooltip element, so we style this
|
// 'inside' its Tooltip element, so we style this
|
||||||
|
@ -229,7 +234,7 @@ const createSpellingSuggestionList = (
|
||||||
for (const suggestion of word.suggestions.slice(0, ITEMS_TO_SHOW)) {
|
for (const suggestion of word.suggestions.slice(0, ITEMS_TO_SHOW)) {
|
||||||
const li = makeLinkItem(suggestion, event => {
|
const li = makeLinkItem(suggestion, event => {
|
||||||
const text = (event.target as HTMLElement).innerText
|
const text = (event.target as HTMLElement).innerText
|
||||||
handleCorrectWord(mark, word, text, view)
|
handleCorrectWord(word, text, view)
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
})
|
})
|
||||||
list.appendChild(li)
|
list.appendChild(li)
|
||||||
|
@ -289,19 +294,19 @@ const handleLearnWord = async function (word: Word, view: EditorView) {
|
||||||
* Correct a word, removing the marked range
|
* Correct a word, removing the marked range
|
||||||
* and replacing it with the chosen text
|
* and replacing it with the chosen text
|
||||||
*/
|
*/
|
||||||
const handleCorrectWord = (
|
const handleCorrectWord = (word: Word, text: string, view: EditorView) => {
|
||||||
mark: Mark,
|
const tooltip = view.state.field(spellingMenuField)
|
||||||
word: Word,
|
if (!tooltip) {
|
||||||
text: string,
|
throw new Error('No active tooltip')
|
||||||
view: EditorView
|
}
|
||||||
) => {
|
const existingText = view.state.doc.sliceString(tooltip.pos, tooltip.end)
|
||||||
const existingText = view.state.doc.sliceString(mark.from, mark.to)
|
|
||||||
// Defend against erroneous replacement, if the word at this
|
// Defend against erroneous replacement, if the word at this
|
||||||
// position is not actually what we think it is
|
// position is not actually what we think it is
|
||||||
if (existingText !== word.text) {
|
if (existingText !== word.text) {
|
||||||
console.debug(
|
console.debug(
|
||||||
'>> spelling word-to-correct does not match, stopping',
|
'>> spelling word-to-correct does not match, stopping',
|
||||||
mark,
|
tooltip.pos,
|
||||||
|
tooltip.end,
|
||||||
existingText,
|
existingText,
|
||||||
word
|
word
|
||||||
)
|
)
|
||||||
|
@ -310,8 +315,8 @@ const handleCorrectWord = (
|
||||||
view.dispatch({
|
view.dispatch({
|
||||||
changes: [
|
changes: [
|
||||||
{
|
{
|
||||||
from: mark.from,
|
from: tooltip.pos,
|
||||||
to: mark.to,
|
to: tooltip.end,
|
||||||
insert: text,
|
insert: text,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in a new issue