autocomplete without react
This commit is contained in:
parent
1b55a5e3e0
commit
47aa4cf198
3 changed files with 48 additions and 36 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
import { createRoot } from 'react-dom/client';
|
|
||||||
import jsdoc from '../../doc.json';
|
import jsdoc from '../../doc.json';
|
||||||
// import { javascriptLanguage } from '@codemirror/lang-javascript';
|
// import { javascriptLanguage } from '@codemirror/lang-javascript';
|
||||||
import { autocompletion } from '@codemirror/autocomplete';
|
import { autocompletion } from '@codemirror/autocomplete';
|
||||||
|
import { h } from './html';
|
||||||
|
|
||||||
const getDocLabel = (doc) => doc.name || doc.longname;
|
const getDocLabel = (doc) => doc.name || doc.longname;
|
||||||
const getInnerText = (html) => {
|
const getInnerText = (html) => {
|
||||||
|
|
@ -10,36 +10,32 @@ const getInnerText = (html) => {
|
||||||
return div.textContent || div.innerText || '';
|
return div.textContent || div.innerText || '';
|
||||||
};
|
};
|
||||||
|
|
||||||
export function Autocomplete({ doc }) {
|
function Autocomplete({ doc }) {
|
||||||
return (
|
return h`<div class="prose dark:prose-invert max-h-[400px] overflow-auto">
|
||||||
<div className="prose dark:prose-invert max-h-[400px] overflow-auto">
|
<h1 class="pt-0 mt-0">${getDocLabel(doc)}</h1>
|
||||||
<h3 className="pt-0 mt-0">{getDocLabel(doc)}</h3>
|
${doc.description}
|
||||||
<div dangerouslySetInnerHTML={{ __html: doc.description }} />
|
<ul>
|
||||||
<ul>
|
${doc.params?.map(
|
||||||
{doc.params?.map(({ name, type, description }, i) => (
|
({ name, type, description }) =>
|
||||||
<li key={i}>
|
`<li>${name} : ${type.names?.join(' | ')} ${description ? ` - ${getInnerText(description)}` : ''}</li>`,
|
||||||
{name} : {type.names?.join(' | ')} {description ? <> - {getInnerText(description)}</> : ''}
|
)}
|
||||||
</li>
|
</ul>
|
||||||
))}
|
<div>
|
||||||
</ul>
|
${doc.examples?.map((example) => `<div><pre>${example}</pre></div>`)}
|
||||||
<div>
|
</div>
|
||||||
{doc.examples?.map((example, i) => (
|
</div>`[0];
|
||||||
<div key={i}>
|
/*
|
||||||
<pre
|
<pre
|
||||||
className="cursor-pointer"
|
className="cursor-pointer"
|
||||||
onMouseDown={(e) => {
|
onMouseDown={(e) => {
|
||||||
console.log('ola!');
|
console.log('ola!');
|
||||||
navigator.clipboard.writeText(example);
|
navigator.clipboard.writeText(example);
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{example}
|
{example}
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
*/
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const jsdocCompletions = jsdoc.docs
|
const jsdocCompletions = jsdoc.docs
|
||||||
|
|
@ -56,9 +52,8 @@ const jsdocCompletions = jsdoc.docs
|
||||||
// detail: 'xxx', // An optional short piece of information to show (with a different style) after the label.
|
// detail: 'xxx', // An optional short piece of information to show (with a different style) after the label.
|
||||||
info: () => {
|
info: () => {
|
||||||
const node = document.createElement('div');
|
const node = document.createElement('div');
|
||||||
// if Autocomplete is non-interactive, it could also be rendered at build time..
|
const ac = Autocomplete({ doc });
|
||||||
// .. using renderToStaticMarkup
|
node.appendChild(ac);
|
||||||
createRoot(node).render(<Autocomplete doc={doc} />);
|
|
||||||
return node;
|
return node;
|
||||||
},
|
},
|
||||||
type: 'function', // https://codemirror.net/docs/ref/#autocomplete.Completion.type
|
type: 'function', // https://codemirror.net/docs/ref/#autocomplete.Completion.type
|
||||||
|
|
@ -6,7 +6,7 @@ import { defaultHighlightStyle, syntaxHighlighting } from '@codemirror/language'
|
||||||
import { Compartment, EditorState, Prec } from '@codemirror/state';
|
import { Compartment, EditorState, Prec } from '@codemirror/state';
|
||||||
import { EditorView, highlightActiveLineGutter, highlightActiveLine, keymap, lineNumbers } from '@codemirror/view';
|
import { EditorView, highlightActiveLineGutter, highlightActiveLine, keymap, lineNumbers } from '@codemirror/view';
|
||||||
import { Pattern, Drawer, repl, cleanupDraw } from '@strudel.cycles/core';
|
import { Pattern, Drawer, repl, cleanupDraw } from '@strudel.cycles/core';
|
||||||
// import { isAutoCompletionEnabled } from './Autocomplete';
|
import { isAutoCompletionEnabled } from './autocomplete.mjs';
|
||||||
import { flash, isFlashEnabled } from './flash.mjs';
|
import { flash, isFlashEnabled } from './flash.mjs';
|
||||||
import { highlightMiniLocations, isPatternHighlightingEnabled, updateMiniLocations } from './highlight.mjs';
|
import { highlightMiniLocations, isPatternHighlightingEnabled, updateMiniLocations } from './highlight.mjs';
|
||||||
import { keybindings } from './keybindings.mjs';
|
import { keybindings } from './keybindings.mjs';
|
||||||
|
|
@ -18,7 +18,7 @@ const extensions = {
|
||||||
isLineWrappingEnabled: (on) => (on ? EditorView.lineWrapping : []),
|
isLineWrappingEnabled: (on) => (on ? EditorView.lineWrapping : []),
|
||||||
isLineNumbersDisplayed: (on) => (on ? lineNumbers() : []),
|
isLineNumbersDisplayed: (on) => (on ? lineNumbers() : []),
|
||||||
theme,
|
theme,
|
||||||
// isAutoCompletionEnabled,
|
isAutoCompletionEnabled,
|
||||||
isPatternHighlightingEnabled,
|
isPatternHighlightingEnabled,
|
||||||
isActiveLineHighlighted: (on) => (on ? [highlightActiveLine(), highlightActiveLineGutter()] : []),
|
isActiveLineHighlighted: (on) => (on ? [highlightActiveLine(), highlightActiveLineGutter()] : []),
|
||||||
isFlashEnabled,
|
isFlashEnabled,
|
||||||
|
|
|
||||||
17
packages/codemirror/html.mjs
Normal file
17
packages/codemirror/html.mjs
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
const parser = new DOMParser();
|
||||||
|
export let html = (string) => {
|
||||||
|
return parser.parseFromString(string, 'text/html').querySelectorAll('*');
|
||||||
|
};
|
||||||
|
let parseChunk = (chunk) => {
|
||||||
|
if (Array.isArray(chunk)) return chunk.flat().join('');
|
||||||
|
if (chunk === undefined) return '';
|
||||||
|
return chunk;
|
||||||
|
};
|
||||||
|
export let h = (strings, ...vars) => {
|
||||||
|
let string = '';
|
||||||
|
for (let i in strings) {
|
||||||
|
string += parseChunk(strings[i]);
|
||||||
|
string += parseChunk(vars[i]);
|
||||||
|
}
|
||||||
|
return html(string);
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue