@neocodemirror/svelte
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -16,3 +16,8 @@ import { defaultKeymap, indentWithTab } from '@codemirror/commands'; | ||
throw new Error("No options provided. At least `value` is required."); | ||
let { value, instanceStore, diagnostics } = options; | ||
let { | ||
value, | ||
instanceStore, | ||
diagnostics, | ||
onChangeBehavior = { kind: "debounce", duration: 50 } | ||
} = options; | ||
let fulfill_editor_initialized; | ||
@@ -46,2 +51,4 @@ let editor_initialized = new Promise((r) => fulfill_editor_initialized = r); | ||
const new_value = view.state.doc.toString(); | ||
if (new_value === value) | ||
return; | ||
if (new_value !== value) { | ||
@@ -56,3 +63,4 @@ value = new_value; | ||
} | ||
const on_change = debounce(handle_change, 50); | ||
const { kind: behaviorKind = "debounce", duration: behaviorDuration = 50 } = onChangeBehavior; | ||
let on_change = behaviorKind === "debounce" ? debounce(handle_change, behaviorDuration) : throttle(handle_change, behaviorDuration); | ||
(async () => { | ||
@@ -134,2 +142,6 @@ internal_extensions = await make_extensions(options); | ||
}); | ||
const { kind: behaviorKind2 = "debounce", duration: behaviorDuration2 = 50 } = new_options.onChangeBehavior ?? { kind: "debounce", duration: 50 }; | ||
if (options.onChangeBehavior?.kind !== behaviorKind2 || options.onChangeBehavior.duration !== behaviorDuration2) { | ||
on_change = behaviorKind2 === "debounce" ? debounce(handle_change, behaviorDuration2) : throttle(handle_change, behaviorDuration2); | ||
} | ||
options = new_options; | ||
@@ -208,3 +220,25 @@ }, | ||
} | ||
function throttle(func, threshold) { | ||
let lastArgs; | ||
let shouldWait = false; | ||
function timeoutFunction(self) { | ||
if (lastArgs) { | ||
func.apply(self, lastArgs); | ||
setTimeout(timeoutFunction, threshold, self); | ||
return; | ||
} | ||
shouldWait = false; | ||
} | ||
return function throttled(...args) { | ||
const self = this; | ||
if (shouldWait) { | ||
lastArgs = args; | ||
return; | ||
} | ||
func.apply(self, args); | ||
shouldWait = true; | ||
setTimeout(timeoutFunction, threshold, self); | ||
}; | ||
} | ||
export { codemirror, withCodemirrorInstance }; |
@@ -314,2 +314,18 @@ import * as _codemirror_autocomplete from '@codemirror/autocomplete'; | ||
onChange?: (e: Transaction) => void; | ||
/** | ||
* Options to config the behavior of the onChange/onTextChange callback. You can specify a kind | ||
* between throttle and debounce and a duration as a number of milliseconds. This prevent the callback from being called | ||
* too many times either by debouncing the change handler or by throttling it. | ||
* | ||
* @default { kind: 'debounce', duration: 50 } | ||
* | ||
* @example | ||
* ```svelte | ||
* <div use:codemirror={{ onChangeBehavior: { kind: 'throttle', duration: 350 } /> | ||
* ``` | ||
*/ | ||
onChangeBehavior?: { | ||
kind?: 'debounce' | 'throttle'; | ||
duration?: number; | ||
}; | ||
}; | ||
@@ -323,6 +339,6 @@ type CodemirrorInstance = { | ||
declare const codemirror: (node: HTMLElement, options: NeoCodemirrorOptions) => ActionReturn<NeoCodemirrorOptions, { | ||
'on:cmTextChange'?: ((e: CustomEvent<string>) => void) | undefined; | ||
'on:cmChange'?: ((e: CustomEvent<Transaction>) => void) | undefined; | ||
'on:codemirror:textChange'?: ((e: CustomEvent<string>) => void) | undefined; | ||
'on:codemirror:change'?: ((e: CustomEvent<Transaction>) => void) | undefined; | ||
}>; | ||
export { NeoCodemirrorOptions, codemirror, withCodemirrorInstance }; |
@@ -16,3 +16,8 @@ import { defaultKeymap, indentWithTab } from '@codemirror/commands'; | ||
throw new Error("No options provided. At least `value` is required."); | ||
let { value, instanceStore, diagnostics } = options; | ||
let { | ||
value, | ||
instanceStore, | ||
diagnostics, | ||
onChangeBehavior = { kind: "debounce", duration: 50 } | ||
} = options; | ||
let fulfill_editor_initialized; | ||
@@ -46,2 +51,4 @@ let editor_initialized = new Promise((r) => fulfill_editor_initialized = r); | ||
const new_value = view.state.doc.toString(); | ||
if (new_value === value) | ||
return; | ||
if (new_value !== value) { | ||
@@ -56,3 +63,4 @@ value = new_value; | ||
} | ||
const on_change = debounce(handle_change, 50); | ||
const { kind: behaviorKind = "debounce", duration: behaviorDuration = 50 } = onChangeBehavior; | ||
let on_change = behaviorKind === "debounce" ? debounce(handle_change, behaviorDuration) : throttle(handle_change, behaviorDuration); | ||
(async () => { | ||
@@ -134,2 +142,6 @@ internal_extensions = await make_extensions(options); | ||
}); | ||
const { kind: behaviorKind2 = "debounce", duration: behaviorDuration2 = 50 } = new_options.onChangeBehavior ?? { kind: "debounce", duration: 50 }; | ||
if (options.onChangeBehavior?.kind !== behaviorKind2 || options.onChangeBehavior.duration !== behaviorDuration2) { | ||
on_change = behaviorKind2 === "debounce" ? debounce(handle_change, behaviorDuration2) : throttle(handle_change, behaviorDuration2); | ||
} | ||
options = new_options; | ||
@@ -208,3 +220,25 @@ }, | ||
} | ||
function throttle(func, threshold) { | ||
let lastArgs; | ||
let shouldWait = false; | ||
function timeoutFunction(self) { | ||
if (lastArgs) { | ||
func.apply(self, lastArgs); | ||
setTimeout(timeoutFunction, threshold, self); | ||
return; | ||
} | ||
shouldWait = false; | ||
} | ||
return function throttled(...args) { | ||
const self = this; | ||
if (shouldWait) { | ||
lastArgs = args; | ||
return; | ||
} | ||
func.apply(self, args); | ||
shouldWait = true; | ||
setTimeout(timeoutFunction, threshold, self); | ||
}; | ||
} | ||
export { codemirror, withCodemirrorInstance }; |
{ | ||
"name": "@neocodemirror/svelte", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "Svelte Action to add codemirro to your apps 😉", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32157
871