man kan skriva med förkortningar från standardlistan

This commit is contained in:
botvid johansson 2025-02-07 11:51:49 +01:00
parent e4f636334d
commit 7253c87774
3 changed files with 82 additions and 4 deletions

View File

@ -3,9 +3,13 @@
import { browser } from '$app/environment'; import { browser } from '$app/environment';
import { getCurrentWord, insertExpandedPhrase, insertText } from '$lib/textarea/main'; 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 type { Shortform } from '../db/main';
import AccordionTrigger from '$lib/components/ui/accordion/accordion-trigger.svelte';
export let activeShortforms: Array<Shortform>; export let activeShortforms: Array<Shortform>;
let shortforms: Map<string, any>;
let state = { let state = {
capitalizeNext: true, capitalizeNext: true,
currentWord: '' currentWord: ''
@ -15,18 +19,36 @@
let text = ''; let text = '';
onMount(() => { onMount(() => {
shortforms = cacheShortforms([890324]);
if (browser) { if (browser) {
let t = document.getElementById('doc') as HTMLTextAreaElement; let t = document.getElementById('doc') as HTMLTextAreaElement;
if (t != null) { if (t != null) {
textarea = t; textarea = t;
setTimeout(() => { setTimeout(() => {
textarea.focus(); textarea.focus();
console.log('focus the textarae'); console.log('focus the textarea');
}, 25); }, 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 = () => { const change = () => {
if (textarea != null) { if (textarea != null) {
if (textarea.value.length == 0) { if (textarea.value.length == 0) {
@ -34,6 +56,7 @@
} }
} }
}; };
const input = (e: InputEvent) => { const input = (e: InputEvent) => {
if (e.inputType == 'insertText') { if (e.inputType == 'insertText') {
const key = e.data || ''; const key = e.data || '';
@ -43,8 +66,7 @@
state.capitalizeNext = false; state.capitalizeNext = false;
e.preventDefault(); e.preventDefault();
} }
//expand(e); expand(e);
console.log(activeShortforms);
} }
if (e.inputType == 'insertText') { if (e.inputType == 'insertText') {
} }

View File

@ -19,7 +19,6 @@ const findIndexOfCurrentWord = (textarea: HTMLTextAreaElement) => {
startIndex >= 0 && startIndex >= 0 &&
/\p{Letter}|\p{Number}/u.test(currentValue[startIndex]) /\p{Letter}|\p{Number}/u.test(currentValue[startIndex])
) { ) {
console.log(currentValue[startIndex]);
startIndex--; startIndex--;
} }
return startIndex; return startIndex;

57
src/modules/shortforms.ts Normal file
View 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 "";
}