@neocodemirror/svelte
Advanced tools
Comparing version 0.0.5 to 0.0.6
import { defaultKeymap, indentWithTab } from '@codemirror/commands'; | ||
import { indentUnit } from '@codemirror/language'; | ||
import { StateEffect, EditorState } from '@codemirror/state'; | ||
import { Compartment, EditorState } from '@codemirror/state'; | ||
import { EditorView, keymap } from '@codemirror/view'; | ||
import { dequal } from 'dequal'; | ||
import { map } from 'nanostores'; | ||
// src/index.ts | ||
var diagnosticsModule; | ||
var withCodemirrorInstance = () => map({ | ||
view: null, | ||
extensions: null, | ||
value: null | ||
}); | ||
var codemirror = (node, options) => { | ||
if (!options) | ||
throw new Error("No options provided. At least `value` is required."); | ||
let { value, instanceStore, diagnostics } = options; | ||
let fulfill_editor_initialized; | ||
let editor_initialized = new Promise((r) => fulfill_editor_initialized = r); | ||
let editor; | ||
let internal_extensions = []; | ||
function handle_change() { | ||
const new_value = editor.state.doc.toString(); | ||
if (new_value === value) | ||
return; | ||
value = new_value; | ||
node.dispatchEvent(new CustomEvent("codemirror:change", { detail: value })); | ||
instanceStore?.setKey("value", value); | ||
} | ||
const on_change = debounce(handle_change, 50); | ||
(async () => { | ||
internal_extensions = await make_extensions(options); | ||
editor = new EditorView({ | ||
doc: value, | ||
extensions: internal_extensions, | ||
parent: node, | ||
dispatch(tr) { | ||
editor.update([tr]); | ||
if (tr.docChanged) { | ||
on_change(); | ||
} | ||
} | ||
}); | ||
make_diagnostics(editor, diagnostics); | ||
instanceStore?.set({ | ||
view: editor, | ||
extensions: internal_extensions, | ||
value | ||
}); | ||
fulfill_editor_initialized(); | ||
})(); | ||
return { | ||
async update(new_options) { | ||
await editor_initialized; | ||
if (value !== new_options.value) { | ||
value = new_options.value; | ||
editor.dispatch({ | ||
changes: { | ||
from: 0, | ||
to: editor.state.doc.length, | ||
insert: value | ||
} | ||
}); | ||
} | ||
if (!dequal( | ||
omit(options, ["value", "diagnostics"]), | ||
omit(new_options, ["value", "diagnostics"]) | ||
)) { | ||
internal_extensions = await make_extensions(new_options); | ||
editor.dispatch({ | ||
effects: StateEffect.reconfigure.of(internal_extensions), | ||
selection: editor.state.selection | ||
}); | ||
} | ||
if (!dequal(options.diagnostics, new_options.diagnostics)) { | ||
make_diagnostics(editor, new_options.diagnostics); | ||
} | ||
instanceStore?.set({ | ||
view: editor, | ||
extensions: internal_extensions, | ||
value | ||
}); | ||
}, | ||
destroy() { | ||
editor_initialized.then(() => { | ||
editor?.destroy(); | ||
}); | ||
} | ||
}; | ||
}; | ||
async function make_diagnostics(editor, diagnostics) { | ||
if (!diagnostics) | ||
return; | ||
if (!diagnosticsModule) | ||
diagnosticsModule = await import('@codemirror/lint'); | ||
const tr = diagnosticsModule.setDiagnostics(editor.state, diagnostics ?? []); | ||
editor.dispatch(tr); | ||
} | ||
async function make_extensions({ | ||
lang, | ||
langMap, | ||
setup, | ||
useTabs, | ||
tabSize = 2, | ||
theme, | ||
styles, | ||
extensions, | ||
readonly | ||
}) { | ||
const internal_extensions = [ | ||
keymap.of([...defaultKeymap, ...useTabs ? [indentWithTab] : []]), | ||
EditorState.tabSize.of(tabSize), | ||
indentUnit.of(useTabs ? " " : " ".repeat(tabSize)) | ||
]; | ||
await do_setup(internal_extensions, { setup }); | ||
if (lang) { | ||
if (typeof lang === "string") { | ||
if (!langMap) | ||
throw new Error("`langMap` is required when `lang` is a string."); | ||
if (!(lang in langMap)) | ||
throw new Error(`Language "${lang}" is not defined in \`langMap\`.`); | ||
const lang_support = await langMap[lang](); | ||
internal_extensions.push(lang_support); | ||
} else { | ||
internal_extensions.push(lang); | ||
} | ||
} | ||
if (theme) | ||
internal_extensions.push(theme); | ||
if (styles) | ||
internal_extensions.push(EditorView.theme(styles)); | ||
if (readonly) | ||
internal_extensions.push(EditorView.editable.of(false)); | ||
return [internal_extensions, extensions ?? []]; | ||
} | ||
async function do_setup(extensions, { setup }) { | ||
if (setup) { | ||
const cm = await import('codemirror'); | ||
if (setup === "basic") { | ||
extensions.push(cm.basicSetup); | ||
} else if (setup === "minimal") { | ||
extensions.push(cm.minimalSetup); | ||
} else { | ||
throw new Error( | ||
"`setup` can only be `basic` or `minimal`. If you wish to provide another setup, pass through `extensions` prop." | ||
); | ||
} | ||
} | ||
} | ||
function debounce(func, threshold, execAsap = false) { | ||
let timeout; | ||
return function debounced(...args) { | ||
const self = this; | ||
if (timeout) | ||
clearTimeout(timeout); | ||
else if (execAsap) | ||
func.apply(self, args); | ||
timeout = setTimeout(delayed, threshold || 100); | ||
function delayed() { | ||
if (!execAsap) | ||
func.apply(self, args); | ||
timeout = null; | ||
} | ||
}; | ||
} | ||
function omit(obj, values) { | ||
const new_obj = Object.assign({}, obj); | ||
for (const value of values) { | ||
delete new_obj[value]; | ||
} | ||
return new_obj; | ||
} | ||
var h,J=()=>map({view:null,extensions:null,value:null}),Q=(n,t)=>{if(!t)throw new Error("No options provided. At least `value` is required.");let {value:r,instanceStore:a,diagnostics:v}=t,o,c=new Promise(e=>o=e),i,f=[],l=new Compartment,d=new Compartment,m=new Compartment,p=new Compartment,g=new Compartment,y=new Compartment;async function C({lang:e,langMap:P,setup:I,useTabs:x,tabSize:K=2,theme:L,styles:k,extensions:V,readonly:j}){return [keymap.of([...defaultKeymap,...x?[indentWithTab]:[]]),l.of(await S(I)??[]),d.of(await T(e,P)),m.of(E(L,k)),p.of(await b(x,K)),g.of(EditorView.editable.of(!j)),y.of(V??[])]}function M(){let e=i.state.doc.toString();e!==r&&(r=e,n.dispatchEvent(new CustomEvent("codemirror:change",{detail:r})),a?.setKey("value",r));}let z=N(M,50);return (async()=>(f=await C(t),i=new EditorView({doc:r,extensions:f,parent:n,dispatch(e){i.update([e]),e.docChanged&&(z(),!0);}}),w(i,v),a?.set({view:i,extensions:f,value:r}),o()))(),{async update(e){await c,r!==e.value&&(r=e.value,i.dispatch({changes:{from:0,to:i.state.doc.length,insert:r}})),e.setup?t.setup!==e.setup&&i.dispatch({effects:l.reconfigure(await S(e.setup))}):i.dispatch({effects:l.reconfigure([])}),e.lang?t.lang!==e.lang&&i.dispatch({effects:d.reconfigure(await T(e.lang,e.langMap))}):i.dispatch({effects:d.reconfigure([])}),e.useTabs||e.tabSize?(t.useTabs!==e.useTabs||t.tabSize!==e.tabSize)&&i.dispatch({effects:p.reconfigure(await b(e.useTabs,e.tabSize))}):i.dispatch({effects:p.reconfigure([await b(!1,2)])}),e.styles||e.theme?t.theme!==e.theme&&i.dispatch({effects:m.reconfigure(E(e.theme,e.styles))}):i.dispatch({effects:m.reconfigure([])}),e.extensions?t.extensions!==e.extensions&&i.dispatch({effects:y.reconfigure(e.extensions)}):i.dispatch({effects:y.reconfigure([])}),e.readonly?t.readonly!==e.readonly&&i.dispatch({effects:g.reconfigure(EditorView.editable.of(!e.readonly))}):i.dispatch({effects:g.reconfigure([])}),w(i,e.diagnostics),a?.set({view:i,extensions:f,value:r}),t=e;},destroy(){c.then(()=>{i?.destroy();});}}};async function S(n){let{basicSetup:t,minimalSetup:r}=await import('codemirror');if(typeof n>"u")return [];if(n==="basic")return t;if(n==="minimal")return r;throw new Error("`setup` can only be `basic` or `minimal`. If you wish to provide another setup, pass through `extensions` prop.")}async function T(n,t){if(typeof n=="string"){if(!t)throw new Error("`langMap` is required when `lang` is a string.");if(!(n in t))throw new Error(`Language "${n}" is not defined in \`langMap\`.`);return await t[n]()}return typeof n>"u"?[]:n}function E(n,t){return [n,t&&EditorView.theme(t)].filter(Boolean)}async function b(n,t=2){return [EditorState.tabSize.of(t),indentUnit.of(n?" ":" ".repeat(t))]}async function w(n,t){if(!t)return;h||(h=await import('@codemirror/lint'));let r=h.setDiagnostics(n.state,t??[]);n.dispatch(r);}function N(n,t,r=!1){let a;return function(...o){let c=this;a?clearTimeout(a):r&&n.apply(c,o),a=setTimeout(i,t||100);function i(){r||n.apply(c,o),a=null;}}} | ||
export { codemirror, withCodemirrorInstance }; | ||
export { Q as codemirror, J as withCodemirrorInstance }; |
@@ -45,20 +45,2 @@ import * as _codemirror_lint from '@codemirror/lint'; | ||
} | ||
/** | ||
* Actions are functions that are called when an element is created. | ||
* You can use this interface to type such actions. | ||
* The following example defines an action that only works on `<div>` elements | ||
* and optionally accepts a parameter which it has a default value for: | ||
* ```ts | ||
* export const myAction: Action<HTMLDivElement, { someProperty: boolean }> = (node, param = { someProperty: true }) => { | ||
* // ... | ||
* } | ||
* ``` | ||
* You can return an object with methods `update` and `destroy` from the function and type which additional attributes and events it has. | ||
* See interface `ActionReturn` for more details. | ||
* | ||
* Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action | ||
*/ | ||
interface Action<Element = HTMLElement, Parameter = any, Attributes extends Record<string, any> = Record<never, any>> { | ||
<Node extends Element>(node: Node, parameter?: Parameter): void | ActionReturn<Parameter, Attributes>; | ||
} | ||
@@ -89,3 +71,3 @@ type MaybePromise<T> = T | Promise<T>; | ||
declare const withCodemirrorInstance: () => MapStore<CodemirrorInstance>; | ||
declare const codemirror: Action<HTMLElement, Options, { | ||
declare const codemirror: (node: HTMLElement, options: Options) => ActionReturn<Options, { | ||
'on:codemirror:change': (e: CustomEvent<string>) => void; | ||
@@ -92,0 +74,0 @@ }>; |
import { defaultKeymap, indentWithTab } from '@codemirror/commands'; | ||
import { indentUnit } from '@codemirror/language'; | ||
import { StateEffect, EditorState } from '@codemirror/state'; | ||
import { Compartment, EditorState } from '@codemirror/state'; | ||
import { EditorView, keymap } from '@codemirror/view'; | ||
import { dequal } from 'dequal'; | ||
import { map } from 'nanostores'; | ||
// src/index.ts | ||
var diagnosticsModule; | ||
var withCodemirrorInstance = () => map({ | ||
view: null, | ||
extensions: null, | ||
value: null | ||
}); | ||
var codemirror = (node, options) => { | ||
if (!options) | ||
throw new Error("No options provided. At least `value` is required."); | ||
let { value, instanceStore, diagnostics } = options; | ||
let fulfill_editor_initialized; | ||
let editor_initialized = new Promise((r) => fulfill_editor_initialized = r); | ||
let editor; | ||
let internal_extensions = []; | ||
function handle_change() { | ||
const new_value = editor.state.doc.toString(); | ||
if (new_value === value) | ||
return; | ||
value = new_value; | ||
node.dispatchEvent(new CustomEvent("codemirror:change", { detail: value })); | ||
instanceStore?.setKey("value", value); | ||
} | ||
const on_change = debounce(handle_change, 50); | ||
(async () => { | ||
internal_extensions = await make_extensions(options); | ||
editor = new EditorView({ | ||
doc: value, | ||
extensions: internal_extensions, | ||
parent: node, | ||
dispatch(tr) { | ||
editor.update([tr]); | ||
if (tr.docChanged) { | ||
on_change(); | ||
} | ||
} | ||
}); | ||
make_diagnostics(editor, diagnostics); | ||
instanceStore?.set({ | ||
view: editor, | ||
extensions: internal_extensions, | ||
value | ||
}); | ||
fulfill_editor_initialized(); | ||
})(); | ||
return { | ||
async update(new_options) { | ||
await editor_initialized; | ||
if (value !== new_options.value) { | ||
value = new_options.value; | ||
editor.dispatch({ | ||
changes: { | ||
from: 0, | ||
to: editor.state.doc.length, | ||
insert: value | ||
} | ||
}); | ||
} | ||
if (!dequal( | ||
omit(options, ["value", "diagnostics"]), | ||
omit(new_options, ["value", "diagnostics"]) | ||
)) { | ||
internal_extensions = await make_extensions(new_options); | ||
editor.dispatch({ | ||
effects: StateEffect.reconfigure.of(internal_extensions), | ||
selection: editor.state.selection | ||
}); | ||
} | ||
if (!dequal(options.diagnostics, new_options.diagnostics)) { | ||
make_diagnostics(editor, new_options.diagnostics); | ||
} | ||
instanceStore?.set({ | ||
view: editor, | ||
extensions: internal_extensions, | ||
value | ||
}); | ||
}, | ||
destroy() { | ||
editor_initialized.then(() => { | ||
editor?.destroy(); | ||
}); | ||
} | ||
}; | ||
}; | ||
async function make_diagnostics(editor, diagnostics) { | ||
if (!diagnostics) | ||
return; | ||
if (!diagnosticsModule) | ||
diagnosticsModule = await import('@codemirror/lint'); | ||
const tr = diagnosticsModule.setDiagnostics(editor.state, diagnostics ?? []); | ||
editor.dispatch(tr); | ||
} | ||
async function make_extensions({ | ||
lang, | ||
langMap, | ||
setup, | ||
useTabs, | ||
tabSize = 2, | ||
theme, | ||
styles, | ||
extensions, | ||
readonly | ||
}) { | ||
const internal_extensions = [ | ||
keymap.of([...defaultKeymap, ...useTabs ? [indentWithTab] : []]), | ||
EditorState.tabSize.of(tabSize), | ||
indentUnit.of(useTabs ? " " : " ".repeat(tabSize)) | ||
]; | ||
await do_setup(internal_extensions, { setup }); | ||
if (lang) { | ||
if (typeof lang === "string") { | ||
if (!langMap) | ||
throw new Error("`langMap` is required when `lang` is a string."); | ||
if (!(lang in langMap)) | ||
throw new Error(`Language "${lang}" is not defined in \`langMap\`.`); | ||
const lang_support = await langMap[lang](); | ||
internal_extensions.push(lang_support); | ||
} else { | ||
internal_extensions.push(lang); | ||
} | ||
} | ||
if (theme) | ||
internal_extensions.push(theme); | ||
if (styles) | ||
internal_extensions.push(EditorView.theme(styles)); | ||
if (readonly) | ||
internal_extensions.push(EditorView.editable.of(false)); | ||
return [internal_extensions, extensions ?? []]; | ||
} | ||
async function do_setup(extensions, { setup }) { | ||
if (setup) { | ||
const cm = await import('codemirror'); | ||
if (setup === "basic") { | ||
extensions.push(cm.basicSetup); | ||
} else if (setup === "minimal") { | ||
extensions.push(cm.minimalSetup); | ||
} else { | ||
throw new Error( | ||
"`setup` can only be `basic` or `minimal`. If you wish to provide another setup, pass through `extensions` prop." | ||
); | ||
} | ||
} | ||
} | ||
function debounce(func, threshold, execAsap = false) { | ||
let timeout; | ||
return function debounced(...args) { | ||
const self = this; | ||
if (timeout) | ||
clearTimeout(timeout); | ||
else if (execAsap) | ||
func.apply(self, args); | ||
timeout = setTimeout(delayed, threshold || 100); | ||
function delayed() { | ||
if (!execAsap) | ||
func.apply(self, args); | ||
timeout = null; | ||
} | ||
}; | ||
} | ||
function omit(obj, values) { | ||
const new_obj = Object.assign({}, obj); | ||
for (const value of values) { | ||
delete new_obj[value]; | ||
} | ||
return new_obj; | ||
} | ||
var h,J=()=>map({view:null,extensions:null,value:null}),Q=(n,t)=>{if(!t)throw new Error("No options provided. At least `value` is required.");let {value:r,instanceStore:a,diagnostics:v}=t,o,c=new Promise(e=>o=e),i,f=[],l=new Compartment,d=new Compartment,m=new Compartment,p=new Compartment,g=new Compartment,y=new Compartment;async function C({lang:e,langMap:P,setup:I,useTabs:x,tabSize:K=2,theme:L,styles:k,extensions:V,readonly:j}){return [keymap.of([...defaultKeymap,...x?[indentWithTab]:[]]),l.of(await S(I)??[]),d.of(await T(e,P)),m.of(E(L,k)),p.of(await b(x,K)),g.of(EditorView.editable.of(!j)),y.of(V??[])]}function M(){let e=i.state.doc.toString();e!==r&&(r=e,n.dispatchEvent(new CustomEvent("codemirror:change",{detail:r})),a?.setKey("value",r));}let z=N(M,50);return (async()=>(f=await C(t),i=new EditorView({doc:r,extensions:f,parent:n,dispatch(e){i.update([e]),e.docChanged&&(z(),!0);}}),w(i,v),a?.set({view:i,extensions:f,value:r}),o()))(),{async update(e){await c,r!==e.value&&(r=e.value,i.dispatch({changes:{from:0,to:i.state.doc.length,insert:r}})),e.setup?t.setup!==e.setup&&i.dispatch({effects:l.reconfigure(await S(e.setup))}):i.dispatch({effects:l.reconfigure([])}),e.lang?t.lang!==e.lang&&i.dispatch({effects:d.reconfigure(await T(e.lang,e.langMap))}):i.dispatch({effects:d.reconfigure([])}),e.useTabs||e.tabSize?(t.useTabs!==e.useTabs||t.tabSize!==e.tabSize)&&i.dispatch({effects:p.reconfigure(await b(e.useTabs,e.tabSize))}):i.dispatch({effects:p.reconfigure([await b(!1,2)])}),e.styles||e.theme?t.theme!==e.theme&&i.dispatch({effects:m.reconfigure(E(e.theme,e.styles))}):i.dispatch({effects:m.reconfigure([])}),e.extensions?t.extensions!==e.extensions&&i.dispatch({effects:y.reconfigure(e.extensions)}):i.dispatch({effects:y.reconfigure([])}),e.readonly?t.readonly!==e.readonly&&i.dispatch({effects:g.reconfigure(EditorView.editable.of(!e.readonly))}):i.dispatch({effects:g.reconfigure([])}),w(i,e.diagnostics),a?.set({view:i,extensions:f,value:r}),t=e;},destroy(){c.then(()=>{i?.destroy();});}}};async function S(n){let{basicSetup:t,minimalSetup:r}=await import('codemirror');if(typeof n>"u")return [];if(n==="basic")return t;if(n==="minimal")return r;throw new Error("`setup` can only be `basic` or `minimal`. If you wish to provide another setup, pass through `extensions` prop.")}async function T(n,t){if(typeof n=="string"){if(!t)throw new Error("`langMap` is required when `lang` is a string.");if(!(n in t))throw new Error(`Language "${n}" is not defined in \`langMap\`.`);return await t[n]()}return typeof n>"u"?[]:n}function E(n,t){return [n,t&&EditorView.theme(t)].filter(Boolean)}async function b(n,t=2){return [EditorState.tabSize.of(t),indentUnit.of(n?" ":" ".repeat(t))]}async function w(n,t){if(!t)return;h||(h=await import('@codemirror/lint'));let r=h.setDiagnostics(n.state,t??[]);n.dispatch(r);}function N(n,t,r=!1){let a;return function(...o){let c=this;a?clearTimeout(a):r&&n.apply(c,o),a=setTimeout(i,t||100);function i(){r||n.apply(c,o),a=null;}}} | ||
export { codemirror, withCodemirrorInstance }; | ||
export { Q as codemirror, J as withCodemirrorInstance }; |
{ | ||
"name": "@neocodemirror/svelte", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Svelte Action to add codemirro to your apps 😉", | ||
@@ -44,3 +44,2 @@ "main": "./dist/index.js", | ||
"csstype": "^3.1.2", | ||
"dequal": "^2.0.3", | ||
"nanostores": "^0.8.1" | ||
@@ -47,0 +46,0 @@ }, |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
8
11869
107
1
- Removeddequal@^2.0.3
- Removeddequal@2.0.3(transitive)