man kan skriva med förkortningar från standardlistan
This commit is contained in:
parent
e4f636334d
commit
7253c87774
@ -3,9 +3,13 @@
|
||||
import { browser } from '$app/environment';
|
||||
|
||||
import { getCurrentWord, insertExpandedPhrase, insertText } from '$lib/textarea/main';
|
||||
import { cacheShortforms, expandShortform } from '../modules/shortforms';
|
||||
import { defaultExpanders } from '../modules/keyboard';
|
||||
import type { Shortform } from '../db/main';
|
||||
import AccordionTrigger from '$lib/components/ui/accordion/accordion-trigger.svelte';
|
||||
|
||||
export let activeShortforms: Array<Shortform>;
|
||||
let shortforms: Map<string, any>;
|
||||
let state = {
|
||||
capitalizeNext: true,
|
||||
currentWord: ''
|
||||
@ -15,18 +19,36 @@
|
||||
let text = '';
|
||||
|
||||
onMount(() => {
|
||||
shortforms = cacheShortforms([890324]);
|
||||
if (browser) {
|
||||
let t = document.getElementById('doc') as HTMLTextAreaElement;
|
||||
if (t != null) {
|
||||
textarea = t;
|
||||
setTimeout(() => {
|
||||
textarea.focus();
|
||||
console.log('focus the textarae');
|
||||
console.log('focus the textarea');
|
||||
}, 25);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const expand = (e: InputEvent) => {
|
||||
const expander = e.data as string;
|
||||
// console.log("expander:", expander);
|
||||
if (defaultExpanders.has(expander)) {
|
||||
const expanderRules = defaultExpanders.get(expander)!;
|
||||
const currentWord = getCurrentWord(textarea).trim();
|
||||
const expandedPhrase = expandShortform(shortforms, currentWord);
|
||||
if (expanderRules.fullstop) {
|
||||
// console.log("should capitalize next");
|
||||
state.capitalizeNext = true;
|
||||
}
|
||||
if (expandedPhrase != '') {
|
||||
insertExpandedPhrase(textarea, expander, currentWord.length, expandedPhrase);
|
||||
e.preventDefault();
|
||||
}
|
||||
}
|
||||
};
|
||||
const change = () => {
|
||||
if (textarea != null) {
|
||||
if (textarea.value.length == 0) {
|
||||
@ -34,6 +56,7 @@
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const input = (e: InputEvent) => {
|
||||
if (e.inputType == 'insertText') {
|
||||
const key = e.data || '';
|
||||
@ -43,8 +66,7 @@
|
||||
state.capitalizeNext = false;
|
||||
e.preventDefault();
|
||||
}
|
||||
//expand(e);
|
||||
console.log(activeShortforms);
|
||||
expand(e);
|
||||
}
|
||||
if (e.inputType == 'insertText') {
|
||||
}
|
||||
|
@ -19,7 +19,6 @@ const findIndexOfCurrentWord = (textarea: HTMLTextAreaElement) => {
|
||||
startIndex >= 0 &&
|
||||
/\p{Letter}|\p{Number}/u.test(currentValue[startIndex])
|
||||
) {
|
||||
console.log(currentValue[startIndex]);
|
||||
startIndex--;
|
||||
}
|
||||
return startIndex;
|
||||
|
57
src/modules/shortforms.ts
Normal file
57
src/modules/shortforms.ts
Normal file
@ -0,0 +1,57 @@
|
||||
import { db } from "../db/main"
|
||||
import { type Shortform } from "../db/main"
|
||||
export function cacheShortforms(lists: Array<string> = []) {
|
||||
let shortforms = new Map();
|
||||
// console.log("Caching shortforms with lists:", lists);
|
||||
|
||||
var n = 0;
|
||||
var shortforms_length = 0;
|
||||
var phrases_length = 0;
|
||||
if (lists.length < 1) {
|
||||
default_shortforms.map((sf: ShortformExportType) => {
|
||||
shortforms.set(sf.sf, { phrase: sf.p, last: sf.u });
|
||||
/*
|
||||
n += 1;
|
||||
shortforms_length += sf.sf.length;
|
||||
phrases_length += sf.p.length;
|
||||
*/
|
||||
});
|
||||
/*
|
||||
console.log("Average shortform length:", shortforms_length / n);
|
||||
console.log("Average phrases length:", phrases_length / n);
|
||||
*/
|
||||
}
|
||||
else {
|
||||
lists.forEach(listid => {
|
||||
db.shortforms.where("listid").equals(listid).toArray().then(sfs => {
|
||||
sfs.forEach(sf => {
|
||||
shortforms.set(sf.shortform, sf)
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
return shortforms;
|
||||
}
|
||||
export function expandShortform(
|
||||
cache: Map<string, any>,
|
||||
ShortForm: string,
|
||||
): string {
|
||||
const u = new Date();
|
||||
const shortform = ShortForm.toLowerCase();
|
||||
if (cache.has(shortform)) {
|
||||
const phrase = cache.get(shortform).phrase;
|
||||
cache.set(shortform, { phrase: phrase, last: u });
|
||||
|
||||
if (shortform.match(/(\d+)/) || ShortForm === shortform) {
|
||||
return phrase;
|
||||
} else if (ShortForm === shortform.toUpperCase() && ShortForm.length > 1) {
|
||||
return phrase.toUpperCase();
|
||||
} else if (/^\p{Lu}(\p{L}*)/u.test(ShortForm)) {
|
||||
return phrase.charAt(0).toUpperCase() + phrase.substring(1);
|
||||
} else {
|
||||
return phrase;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user