Add decorator autocomplete

This commit is contained in:
kwaroran
2024-11-02 02:17:12 +09:00
parent 2ca2371b16
commit 552c3db6a7
2 changed files with 20 additions and 7 deletions

View File

@@ -115,7 +115,7 @@
</div>
<script lang="ts">
import { textAreaSize, textAreaTextSize } from 'src/ts/gui/guisize'
import { highlighter, getNewHighlightId, removeHighlight, AllCBS } from 'src/ts/gui/highlight'
import { highlighter, getNewHighlightId, removeHighlight, AllCBS, decorators } from 'src/ts/gui/highlight'
import { isMobile } from 'src/ts/globalApi.svelte';
import { isFirefox, sleep } from 'src/ts/util';
import { onDestroy, onMount } from 'svelte';
@@ -159,7 +159,7 @@
let autocompleteContents:string[] = $state([])
let inputDom: HTMLDivElement = $state()
const autoComplete = () => {
const autoComplete = (type:'cbs'|'decorator'|'both' = 'both') => {
if(isMobile){
return
}
@@ -174,14 +174,24 @@
if(range){
const qValue = (range.startContainer).textContent
const splited = qValue.substring(0, range.startOffset).split('{{')
const splited = qValue.substring(0, range.startOffset).split(type === 'decorator' ? '@@' : '{{')
if(splited.length === 1){
if(type === 'both'){
return autoComplete('decorator')
}
hideAutoComplete()
return
}
const qText = splited.pop()
let filtered = AllCBS.filter((cb) => cb.startsWith(qText))
let filtered = (type === 'decorator' ? decorators : AllCBS).filter((cb) => cb.startsWith(qText))
if(type === 'decorator'){
filtered = filtered.map((cb) => '@' + cb)
}
if(filtered.length === 0){
if(type === 'both'){
return autoComplete('decorator')
}
hideAutoComplete()
return
}
@@ -211,8 +221,11 @@
let contentStart = content.substring(0, range.startOffset)
let contentEnd = content.substring(range.startOffset)
if(type === 'autoComplete'){
contentStart = contentStart.substring(0, contentStart.lastIndexOf('{{'))
if(insertContent.endsWith(':')){
contentStart = contentStart.substring(0, contentStart.lastIndexOf(insertContent.startsWith('@') ? '@@' : '{{'))
if(insertContent.startsWith('@')){
insertContent = `@${insertContent}`
}
else if(insertContent.endsWith(':')){
insertContent = `{{${insertContent}:`
}
else if(insertContent.startsWith('#')){