VibeFox JS Editor — डेमो
त्वरित शुरुआत
सादा HTML
यह त्वरित शुरुआत वाला एडिटर है, जिसमें पूरा टूलबार है और स्टेटस बार में शब्दों की गिनती दिखती है। खाली लाइन में ब्लॉक मेनू के लिए / टाइप करें, फ़ॉर्मैट करने के लिए टेक्स्ट चुनें।
const editor = VibeFoxJSEditor.createEditor('#app', {
data: '<h2>Release notes</h2><p>Type <b>/</b> for blocks, or use the toolbar.</p>',
toolbar: 'full',
placeholder: 'Start typing…',
on: { change: (e) => console.log(e.html) },
});मूल अवधारणाएँ
सामग्री
सामग्री HTML है: एडिटर के नीचे का बॉक्स हर बदलाव के बाद getData(), सादा टेक्स्ट और गिनती दिखाता है। शुरुआती HTML की स्क्रिप्ट, इवेंट हैंडलर और असुरक्षित लिंक को सैनिटाइज़र हटा देता है।
const output = document.createElement('pre');
output.style.cssText = 'height: 120px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const editor = VibeFoxJSEditor.createEditor('#app', {
toolbar: 'basic',
data: '<p>Hello <b>world</b>!<script>alert(1)<\/script></p><p><a href="javascript:alert(2)" onclick="alert(3)">A link</a> to edit.</p>',
});
function show() {
const count = editor.getWordCount();
output.textContent = editor.getData() + '\n\ntext: ' + JSON.stringify(editor.getText()) + '\nwords: ' + count.words + ', chars: ' + count.chars;
}
editor.on('change', show);
show();बनाना और नष्ट करना
बटन इंस्टेंस के मेथड्स को कॉल करते हैं: सामग्री बदलें, उसे केवल-पढ़ने योग्य बनाएँ, ऊँचाई बदलें, एडिटर को नष्ट करें और उसे फिर से बनाएँ।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
let editor = null;
function create() {
editor?.destroy();
editor = VibeFoxJSEditor.createEditor('#app', { data: '<p>Hello</p>', toolbar: 'basic' });
}
create();
addButtons([
['setData', () => editor.setData('<h2>Title</h2><p>Text set by <code>setData()</code>.</p>')],
['Read only', () => editor.setOptions({ readonly: !editor.getOption('readonly') })],
['maxHeight 120px', () => editor.setOption('maxHeight', '120px')],
['Destroy', () => { editor.destroy(); editor = null; }],
['Create', create],
]);कमांड
हर बटन कैरेट की जगह exec() से एक कमांड चलाता है, जो एक अनडू-स्टेप के रूप में दर्ज होता है; आख़िरी बटन transact() से दो इन्सर्ट को एक स्टेप बनाता है। टेक्स्ट में क्लिक करें, फिर कोई बटन दबाएँ; Ctrl+Z उसे वापस ले लेता है।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const editor = VibeFoxJSEditor.createEditor('#app', {
data: '<p>Click into this line, then a button.</p>',
toolbar: [['undo', 'redo'], ['blockFormat'], ['bold', 'italic', 'textColor'], ['table', 'link']],
});
const run = (name, value) => () => { editor.focus(); editor.exec(name, value); };
addButtons([
['h2', run('h2')],
['bold', run('bold')],
['table 3 × 3', run('table', { rows: 3, cols: 3, header: true })],
['link', run('link', { href: 'https://example.com', text: 'Example', newTab: true })],
['blue text', run('textColor', '#2563eb')],
['transact', () => { editor.focus(); editor.transact(() => { editor.insertText('Done. '); editor.insertHTML('<i>Thanks</i>'); }); }],
]);इवेंट
हर इवेंट हैंडलर को एक ही पेलोड ऑब्जेक्ट मिलता है; एडिटर के नीचे की सूची में सबसे नए इवेंट दिखते हैं। टाइप करें, टेक्स्ट चुनें या Ctrl+Z दबाएँ।
const output = document.createElement('pre');
output.style.cssText = 'height: 110px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const editor = VibeFoxJSEditor.createEditor('#app', { data: '<p>Type here or select some words.</p>', toolbar: 'basic' });
const write = (text) => { output.textContent = text + '\n' + output.textContent; };
editor.on('change', (e) => write('change ' + e.source + ', ' + e.html.length + ' chars of HTML'));
editor.on('selectionChange', (e) => write('selectionChange collapsed=' + e.collapsed + ' text=' + JSON.stringify(e.text)));
editor.on('wordCountChange', (e) => write('wordCountChange ' + e.words + ' words'));
editor.on('undo', () => write('undo'));स्थिति के स्नैपशॉट
getState() सामग्री और व्यू (यहाँ: सोर्स मोड और खोज बार) का JSON स्नैपशॉट है। सहेजें, कुछ बदलें, फिर पुनर्स्थापित करें।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const editor = VibeFoxJSEditor.createEditor('#app', { data: '<p>Saved state: this text, the mode and the find bar.</p>', toolbar: 'basic' });
let saved = null;
addButtons([
['Save state', () => { saved = JSON.stringify(editor.getState()); }],
['Change', () => { editor.setData('<p>Something else.</p>'); editor.openFind(); }],
['Source mode', () => editor.setMode(editor.getMode() === 'source' ? 'wysiwyg' : 'source')],
['Restore state', () => { if (saved) editor.setState(JSON.parse(saved)); }],
]);लेखन और फ़ॉर्मैटिंग
टाइपिंग सहायक
मेंशन के लिए @ टाइप करें, खाली लाइन में ब्लॉक मेनू के लिए /, Markdown शॉर्टकट के लिए # , - या **bold**, और स्मार्ट टाइपोग्राफ़ी के लिए कोट्स या --। ऑटोसेव इवेंट एडिटर के नीचे दिखाया जाता है।
const output = document.createElement('pre');
output.style.cssText = 'height: 40px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const users = [{ id: 1, label: 'Alice' }, { id: 2, label: 'Bob' }, { id: 3, label: 'Carol' }];
const notes = VibeFoxJSEditor.createEditor('#app', {
data: '<p>Ask @ about the plan.</p><p><br></p>',
minHeight: '220px',
mentions: { items: async (q) => users.filter((u) => u.label.toLowerCase().startsWith(q.toLowerCase())) },
autosave: { enabled: true, interval: 2000, key: 'demo-notes-draft' },
});
notes.on('mentionInsert', (e) => { output.textContent = 'mention: ' + JSON.stringify(e); });
notes.on('autosave', (e) => { output.textContent = 'autosave at ' + new Date(e.time).toLocaleTimeString() + ': ' + e.html.length + ' chars'; });फ़ॉर्मैटिंग विकल्प
टूलबार के फ़ॉन्ट और साइज़ मेनू में आपके अपने फ़ॉन्ट और साइज़ सूचीबद्ध हैं; Tab सूची-आइटम को नेस्ट करता है। बटन विशेष अक्षरों और इमोजी पैनल खोलते हैं।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const editor = VibeFoxJSEditor.createEditor('#app', {
data: '<p style="font-family: Georgia, serif">A paragraph in Georgia.</p><ul><li>First item</li><li>Second item (press Tab here)</li></ul>',
toolbar: [['fontFamily', 'fontSize', 'lineHeight'], ['bold', 'italic', 'textColor', 'bgColor'], ['ul', 'ol', 'outdent', 'indent'], ['specialChars', 'emoji']],
format: {
fonts: ['Georgia, serif', { label: 'Mono', value: 'ui-monospace, monospace' }, 'system-ui, sans-serif'],
fontSizes: ['12px', '16px', '24px'],
lineHeights: ['1', '1.5', '2'],
indentStep: '1.5em',
},
});
addButtons([
['Special characters', () => { editor.focus(); editor.exec('specialChars'); }],
['Emoji', () => { editor.focus(); editor.exec('emoji'); }],
['Insert ★', () => { editor.focus(); editor.exec('specialChars', '★'); }],
]);पेस्ट
बटन cleanExternalHtml() के ज़रिए Word दस्तावेज़ का HTML पेस्ट करता है: सूची, हेडिंग और तालिका बनी रहती हैं, Word की स्टाइलें हट जाती हैं। Word या Google Docs से अपना टेक्स्ट भी पेस्ट करें।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const editor = VibeFoxJSEditor.createEditor('#app', { data: '<p>Paste below this line.</p>', toolbar: 'basic' });
editor.on('paste', (e) => console.log('paste', e.kind));
const fromWord = '<h1 class="MsoTitle" style="mso-style-name:Title">Quarterly report</h1>' +
'<p class="MsoListParagraphCxSpFirst" style="mso-list:l0 level1 lfo1"><span style="mso-list:Ignore">1.</span> Sales grew</p>' +
'<p class="MsoListParagraphCxSpLast" style="mso-list:l0 level1 lfo1"><span style="mso-list:Ignore">2.</span> Costs fell</p>' +
'<table class="MsoTableGrid" border="1"><tr><td><p class="MsoNormal"><b>Region</b></p></td><td><p class="MsoNormal"><b>Sales</b></p></td></tr>' +
'<tr><td><p class="MsoNormal">Europe</p></td><td><p class="MsoNormal">1 200</p></td></tr></table>';
addButtons([
['Paste Word HTML', () => { editor.focus(); editor.insertHTML(VibeFoxJSEditor.cleanExternalHtml(fromWord)); }],
]);टूलबार और मोड
टूलबार
आपके अपने टूलबार का लेआउट समूहों में है, और अंत में HTML सोर्स व्यू, हेडिंग की रूपरेखा, फ़ोकस मोड और फ़ुल स्क्रीन हैं।
const doc = '<h2>Release notes</h2>' +
'<p>Version <b>2.4</b> brings <i>faster search</i>, a new <a href="https://example.com">export</a> and many small fixes.</p>' +
'<ul><li>Search is three times faster</li><li>Export to PDF and Word</li><li>Dark theme</li></ul>' +
'<blockquote><p>The best release so far. — Ann, product manager</p></blockquote>';
const editor = VibeFoxJSEditor.createEditor('#app', {
data: doc + '<h3>Known issues</h3><p>None so far.</p>',
toolbar: [['undo', 'redo'], ['blockFormat'], ['bold', 'italic', 'link'], ['ul', 'ol', 'checklist'], ['table', 'image'], ['source', 'outline', 'focus', 'fullscreen']],
stickyToolbar: true,
statusbar: true,
});क्लासिक, इनलाइन और बैलून
ऊपर: इनलाइन एडिटर पेज का लुक बनाए रखता है और संपादन के समय ब्लॉक के ऊपर अपना टूलबार दिखाता है। नीचे: बैलून एडिटर चयन पर एक छोटा टूलबार और खाली लाइन पर "+" दिखाता है। टेक्स्ट में क्लिक करें।
const app = document.getElementById('app');
app.innerHTML = '<div id="inline"><h2>About us</h2><p>Click to edit this text in place: the toolbar floats above the block.</p></div>' +
'<hr><div id="balloon"><p>Select a word here for the balloon toolbar.</p><p><br></p></div>';
app.style.cssText = 'padding-top: 48px; font-family: Georgia, serif';
const inline = VibeFoxJSEditor.createEditor('#inline', { ui: 'inline' });
const balloon = VibeFoxJSEditor.createEditor('#balloon', { ui: 'balloon' });कई एडिटरों के लिए एक टूलबार
शीर्षक और मुख्य भाग एक ही टूलबार के अंतर्गत दो इनलाइन एडिटर हैं: टूलबार उसी एडिटर पर काम करता है जिसे फ़ोकस है।
document.querySelector('#app').innerHTML = '<div id="bar"></div><div id="title"></div><div id="body"></div>';
const title = VibeFoxJSEditor.createEditor('#title', { ui: 'inline', data: '<h1>Title</h1>' });
const body = VibeFoxJSEditor.createEditor('#body', { ui: 'inline', data: '<p>Body text. Click here or into the title, then use the toolbar.</p>' });
const bar = VibeFoxJSEditor.createSharedToolbar('#bar', { editors: [title, body], toolbar: 'basic' });
bar.on('activeChange', (e) => console.log('active editor changed'));छवियाँ, फ़ाइलें और एम्बेड
अपलोड
यहाँ का अपलोड एडैप्टर एक स्टब है, जो केवल इंतज़ार करता है और प्रगति दिखाता है (असली एडैप्टर फ़ाइल को आपके सर्वर पर भेजता है)। बटन पर क्लिक करें, टूलबार से फ़ाइल चुनें, या टेक्स्ट में कोई छवि ड्रॉप करें।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const editor = VibeFoxJSEditor.createEditor('#app', {
data: '<p>Images and files go to the upload adapter.</p>',
toolbar: [['bold', 'italic'], ['image', 'file']],
upload: {
// stub: a real adapter sends the file to your server and returns its URL
handler: async (file, { onProgress }) => {
for (let i = 1; i <= 10; i++) { await new Promise((r) => setTimeout(r, 120)); onProgress(i / 10); }
const url = await new Promise((resolve) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.readAsDataURL(file);
});
return file.type.startsWith('image/') ? { url, alt: file.name } : { url, name: file.name };
},
accept: ['image/*', '.pdf', '.txt'],
maxSize: 10e6,
},
});
editor.on('uploadDone', (e) => console.log('uploaded', e.file.name));
const svg = '<svg xmlns="http://www.w3.org/2000/svg" width="240" height="120"><rect width="240" height="120" rx="12" fill="#2563eb"/>' +
'<text x="120" y="68" font-size="22" text-anchor="middle" fill="#fff" font-family="sans-serif">chart.svg</text></svg>';
addButtons([
['Upload a sample image', () => { editor.focus(); editor.uploadFiles([new File([svg], 'chart.svg', { type: 'image/svg+xml' })]); }],
['Upload a text file', () => { editor.focus(); editor.uploadFiles([new File(['Hello'], 'notes.txt', { type: 'text/plain' })]); }],
]);एम्बेड
YouTube का लिंक बिना नेटवर्क अनुरोध के एम्बेड बन जाता है: जब तक प्लेयर लोड करने की अनुमति नहीं मिलती, एडिटर एक प्लेसहोल्डर दिखाता है (embed.live)। खाली लाइन में भी YouTube या Vimeo का लिंक पेस्ट करें।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const editor = VibeFoxJSEditor.createEditor('#app', { data: '<p>The video:</p><p><br></p>', toolbar: [['bold', 'italic'], ['link', 'embed']] });
editor.on('embedInsert', (e) => console.log(e.provider, e.src));
addButtons([
['Insert a YouTube video', async () => {
editor.focus();
const res = await editor.insertEmbed('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
console.log(res.ok, res.provider, res.src); // youtube-nocookie.com player
}],
]);तालिकाएँ
हेडर पंक्ति वाली एक तालिका; कमांड "Pro" के नीचे एक पंक्ति जोड़ते हैं, उसके दो सेल मर्ज करते हैं, उन्हें रंग देते हैं और कॉलम की चौड़ाई सेट करते हैं। कॉलम की सीमा खींचें, तालिका मेनू के लिए किसी सेल पर राइट-क्लिक करें।
const editor = VibeFoxJSEditor.createEditor('#app', {
data: '<p>Prices</p><table><thead><tr><th>Plan</th><th>Month</th><th>Year</th></tr></thead>' +
'<tbody><tr><td>Basic</td><td>$10</td><td>$100</td></tr><tr><td>Pro</td><td>$25</td><td>$250</td></tr></tbody></table>',
});
const cell = (row, col) => editor.content.querySelectorAll('tr')[row].children[col];
const caretIn = (el) => getSelection().setBaseAndExtent(el, 0, el, 0);
editor.focus();
caretIn(cell(2, 0));
editor.exec('rowBelow');
caretIn(cell(3, 0));
editor.exec('mergeRight');
editor.exec('cellProps', { background: '#fef3c7', align: 'center', valign: 'middle' });
editor.exec('columnWidths', [50, 25, 25]);
editor.on('columnResize', (e) => console.log(e.widths));खोजें और बदलें
खोज बार रेगुलर एक्सप्रेशन के साथ खुला है; मिलान हाइलाइट किए गए हैं। बटन दबाएँ, या टेक्स्ट में Ctrl+F / Ctrl+H दबाएँ।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const editor = VibeFoxJSEditor.createEditor('#app', { data: '<p>Color and colour. COLOR! The colors of the color picker.</p>' });
console.log(editor.find('colou?r', { regex: true })); // { total, index }
editor.openFind({ replace: true });
addButtons([
['Next', () => editor.findNext()],
['Replace with "shade"', () => editor.replace('shade')],
['Replace all with "tone"', () => editor.replaceAll('tone', { query: 'colou?r', regex: true, caseSensitive: false })],
['Clear', () => editor.clearFind()],
]);कोड ब्लॉक
कोड ब्लॉक भाषा के अनुसार रंगीन होते हैं: SQL, Python और addCodeLanguage() से जोड़ी गई अपनी INI भाषा। ब्लॉक पर लगा चिप भाषा चुनता है; नया ब्लॉक बनाने के लिए ```js टाइप करें और Enter दबाएँ।
VibeFoxJSEditor.addCodeLanguage({ id: 'ini', label: 'INI', rules: [['comment', /;.*/], ['property', /^[\w.]+(?=\s*=)/m], ['string', /=.*/]], multiline: true });
const editor = VibeFoxJSEditor.createEditor('#app', {
data: '<pre><code class="language-sql">SELECT id, name FROM users WHERE active = 1 ORDER BY name;</code></pre>' +
'<pre><code class="language-python">def area(r):\n # circle\n return 3.14159 * r ** 2</code></pre>' +
'<pre><code class="language-ini">; settings\nhost = example.com\nport = 8080</code></pre>',
codeBlock: { detect: true, defaultLanguage: 'js', languages: ['js', 'ts', 'sql', 'python', 'ini'] },
});
editor.on('codeLanguageChange', (e) => console.log(e.previous, '→', e.language));सूत्र
LaTeX सूत्र MathML के रूप में दिखाए जाते हैं, इनलाइन और ब्लॉक के रूप में। सूत्र पर क्लिक करें ताकि उसे प्रीव्यू के साथ डायलॉग में संपादित किया जा सके, या टूलबार के Σ बटन का उपयोग करें।
const editor = VibeFoxJSEditor.createEditor('#app', { data: '<p>The mean is not greater than the length: </p>' });
const p = editor.content.querySelector('p');
getSelection().setBaseAndExtent(p, p.childNodes.length, p, p.childNodes.length);
editor.focus();
const r = editor.insertMath('\\frac{a+b}{2} \\le \\sqrt{x^2+y^2}');
editor.insertMath('\\sum_{i=1}^{n} i = \\frac{n(n+1)}{2}', { display: true });
console.log(r.ok, editor.getFormulas());
console.log(VibeFoxJSEditor.checkLatex('\\frac{a}').error.code); // 'missingArg'बदलावों का ट्रैकिंग
सुझाव मोड चालू है: Ann के सम्मिलन और विलोपन रंगीन हैं, बगल का पैनल उन्हें सूचीबद्ध करता है। अपने सुझाव जोड़ने के लिए टेक्स्ट टाइप करें या हटाएँ, और पैनल में उन्हें स्वीकार या अस्वीकार करें।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const editor = VibeFoxJSEditor.createEditor('#app', { data: '<p>The quick brown fox jumps over the lazy dog.</p>', minHeight: '260px' });
editor.setOptions({ trackChanges: { enabled: true, author: { id: 'ann', name: 'Ann' } } });
editor.focus();
const text = editor.content.querySelector('p').firstChild;
getSelection().setBaseAndExtent(text, 4, text, 10); // "quick " is replaced: a deletion and an insertion
editor.insertText('swift ');
const p = editor.content.querySelector('p');
getSelection().setBaseAndExtent(p, p.childNodes.length, p, p.childNodes.length);
editor.insertText(' It was fast!');
editor.toggleChangesPanel(true);
console.log(editor.getChanges());
addButtons([
['Accept all', () => editor.acceptAllChanges()],
['Reject all', () => editor.rejectAllChanges()],
['Suggestion mode on / off', () => editor.setTrackChanges(!editor.isTrackingChanges())],
]);टिप्पणियाँ
Bob की टिप्पणी-थ्रेड, जिस पर Ann का जवाब है, वाक्य से जुड़ी है; पैनल थ्रेड दिखाता है। कोई अन्य टेक्स्ट चुनें और एक जोड़ने के लिए Ctrl+Alt+M दबाएँ (या टिप्पणी बटन)।
const editor = VibeFoxJSEditor.createEditor('#app', {
data: '<p>Revenue grew by 12 percent in March.</p><p>Costs stayed flat.</p>',
minHeight: '260px',
comments: { author: { id: 'ann', name: 'Ann' } },
});
const range = document.createRange();
range.selectNodeContents(editor.content.querySelector('p'));
const thread = editor.addComment('Source of this number?', { range, author: { id: 'bob', name: 'Bob' } });
editor.addReply(thread.id, 'From the March report.', { author: { id: 'ann', name: 'Ann' } });
editor.toggleCommentsPanel(true);
editor.on('commentsChange', () => console.log(editor.getComments()));संस्करण और तुलना
दो सहेजे गए संस्करण साथ-साथ तुलना किए गए: बाईं ओर ड्राफ़्ट के हटाए गए शब्द, दाईं ओर जोड़े गए। बटन व्यू बदलते हैं और ड्राफ़्ट पुनर्स्थापित करते हैं।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const editor = VibeFoxJSEditor.createEditor('#app', { data: '<p>First draft of the text.</p><p>It has two short paragraphs.</p>' });
const v1 = editor.saveVersion({ name: 'Draft' });
editor.setData('<p>Second draft of the long text.</p><p>It has two short paragraphs and a list:</p><ul><li>one</li><li>two</li></ul>');
editor.saveVersion({ name: 'Review' });
const cmp = editor.compareVersions(v1.id, 'current');
console.log(cmp.stats);
editor.showCompare(v1.id, 'current', { layout: 'side' });
addButtons([
['Side by side', () => editor.showCompare(v1.id, 'current', { layout: 'side' })],
['Inline', () => editor.showCompare(v1.id, 'current', { layout: 'inline' })],
['Close', () => editor.hideCompare()],
['Versions panel', () => editor.toggleVersionsPanel()],
['Restore the draft', () => { editor.hideCompare(); editor.restoreVersion(v1.id); }],
]);रीयल-टाइम सहयोग
Ann और Bob दो एडिटरों में एक ही दस्तावेज़ संपादित करते हैं; यहाँ ट्रांसपोर्ट सर्वर की जगह पेज में एक छोटा रिले है। एक एडिटर में टाइप करें: टेक्स्ट, कर्सर और नाम-टैग दूसरे एडिटर में दिखाई देते हैं।
const app = document.getElementById('app');
app.innerHTML = '<div id="ann"></div><div id="bob" style="margin-top: 8px"></div>';
// the relay: in a real page send() goes to your WebSocket / WebRTC / server
const members = [];
function relay() {
const me = { listeners: [] };
members.push(me);
return {
send: (msg) => {
const copy = JSON.parse(JSON.stringify(msg));
setTimeout(() => { for (const m of members) if (m !== me) m.listeners.forEach((fn) => fn(copy)); }, 50);
},
onMessage: (fn) => { me.listeners.push(fn); },
};
}
const data = '<h3>Meeting notes</h3><p>Agenda: the release of version 2.4.</p><ul><li>Ann: the release notes</li><li>Bob: the tests</li></ul>';
const ann = VibeFoxJSEditor.createEditor('#ann', { data, toolbar: 'basic', minHeight: '120px' });
const bob = VibeFoxJSEditor.createEditor('#bob', { data, toolbar: 'basic', minHeight: '120px' });
const sessionA = ann.collaborate({ room: 'notes', transport: relay(), user: { name: 'Ann', color: 1 } });
const sessionB = bob.collaborate({ room: 'notes', transport: relay(), user: { name: 'Bob', color: 4 } });
bob.on('peersChange', (e) => console.log('Bob sees', e.peers.map((p) => p.name)));
// Ann types a word to show the remote cursor
setTimeout(() => {
ann.focus();
const li = ann.content.querySelector('li');
getSelection().setBaseAndExtent(li.firstChild, li.firstChild.length, li.firstChild, li.firstChild.length);
ann.insertText(' (draft ready)');
}, 300);आयात और निर्यात
HTML और Markdown
बटन दस्तावेज़ को HTML या Markdown के रूप में डाउनलोड करते हैं, एडिटर के नीचे Markdown दिखाते हैं और टास्क-लिस्ट और तालिका वाला Markdown आयात करते हैं।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const doc = '<h2>Release notes</h2>' +
'<p>Version <b>2.4</b> brings <i>faster search</i>, a new <a href="https://example.com">export</a> and many small fixes.</p>' +
'<ul><li>Search is three times faster</li><li>Export to PDF and Word</li><li>Dark theme</li></ul>' +
'<blockquote><p>The best release so far. — Ann, product manager</p></blockquote>';
const output = document.createElement('pre');
output.style.cssText = 'height: 90px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const editor = VibeFoxJSEditor.createEditor('#app', { data: doc, toolbar: 'basic' });
addButtons([
['Download HTML', () => editor.exportHTML({ download: true, filename: 'release-notes.html' })],
['Download Markdown', () => editor.exportMarkdown({ download: true, filename: 'release-notes.md' })],
['Show Markdown', () => { output.textContent = editor.exportMarkdown(); }],
['Import Markdown', () => editor.importMarkdown('# Title\n\n- [x] done\n- [ ] todo\n\n| a | b |\n|---|---|\n| 1 | 2 |')],
]);PDF ब्राउज़र में बनाया जाता है: वेक्टर पेज, जिनका टेक्स्ट चुना जा सकता है, हेडर और पेज नंबर के साथ। उसे डाउनलोड करने के लिए बटन पर क्लिक करें।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const doc = '<h2>Release notes</h2>' +
'<p>Version <b>2.4</b> brings <i>faster search</i>, a new <a href="https://example.com">export</a> and many small fixes.</p>' +
'<ul><li>Search is three times faster</li><li>Export to PDF and Word</li><li>Dark theme</li></ul>' +
'<blockquote><p>The best release so far. — Ann, product manager</p></blockquote>';
const editor = VibeFoxJSEditor.createEditor('#app', {
data: doc + '<table><tr><th>Region</th><th>Sales</th></tr><tr><td>Europe</td><td>1 200</td></tr><tr><td>America</td><td>950</td></tr></table>',
});
const info = addButtons([
['Download PDF', async () => {
const bytes = await editor.exportPDF('release-notes.pdf', {
pageSize: 'A4', header: '{title}', footer: { text: 'Page {page} of {pages}', align: 'center' }, title: 'Release notes',
});
info.lastChild.textContent = 'release-notes.pdf: ' + Math.round(bytes.length / 1024) + ' KB';
}],
]);
info.append(document.createElement('span'));Word (.docx)
सुझाव और टिप्पणी वाला दस्तावेज़ असली रिविज़न और टिप्पणियों के साथ Word फ़ाइल के रूप में डाउनलोड होता है; "Import" .docx को वापस एडिटर में पढ़ता है।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const editor = VibeFoxJSEditor.createEditor('#app', {
data: '<h2>Contract</h2><p>The term of the contract is one year.</p>',
trackChanges: { author: { id: 'ann', name: 'Ann' } },
});
editor.setTrackChanges(true);
editor.focus();
editor.insertText(' It renews every year.');
editor.setTrackChanges(false);
const range = document.createRange();
range.selectNodeContents(editor.content.querySelector('h2'));
editor.addComment('Use the full name of the contract.', { range, author: { id: 'bob', name: 'Bob' } });
const info = addButtons([
['Download .docx', () => editor.exportDOCX('contract.docx', { changes: 'markup', comments: true })],
['Import the .docx back', async () => {
const docx = await editor.getDOCX({ changes: 'markup', comments: true, font: 'Calibri' });
const result = await editor.importDOCX(docx, { changes: 'markup', comments: true, images: 'inline' });
info.lastChild.textContent = 'imported ' + Math.round(docx.length / 1024) + ' KB, warnings: ' + result.warnings.length;
}],
]);
info.append(document.createElement('span'));पेज व्यू और प्रिंटिंग
दस्तावेज़ Letter शीट पर है, हेडर, पेज नंबर और पेज ब्रेक के साथ, 60% ज़ूम पर। दूसरा पेज ब्रेक डालने के लिए Ctrl+Enter दबाएँ; "Print" शीट प्रिंट करता है।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const doc = '<h2>Release notes</h2>' +
'<p>Version <b>2.4</b> brings <i>faster search</i>, a new <a href="https://example.com">export</a> and many small fixes.</p>' +
'<ul><li>Search is three times faster</li><li>Export to PDF and Word</li><li>Dark theme</li></ul>' +
'<blockquote><p>The best release so far. — Ann, product manager</p></blockquote>';
const editor = VibeFoxJSEditor.createEditor('#app', { data: doc + '<p>More text on the second page.</p>', maxHeight: '520px' });
editor.setPages({ size: 'Letter', orientation: 'portrait', margin: '2cm', header: 'Release notes', pageNumbers: true,
firstPage: { header: null }, zoom: 0.6 });
const p = editor.content.querySelectorAll('p')[1];
getSelection().setBaseAndExtent(p, 0, p, 0);
editor.focus();
editor.exec('pageBreak');
console.log(editor.getPageCount(), editor.getCurrentPage(), editor.getZoom());
addButtons([
['Zoom 100%', () => editor.setZoom(1)],
['Fit width', () => editor.setZoom('width')],
['Print', () => editor.print()],
]);AI सहायक
यहाँ का एडैप्टर पेज में एक स्टब मॉडल है, जो तैयार उत्तर स्ट्रीम करता है (असली मॉडल आपके मॉडल को कॉल करता है)। "Summarize" पर क्लिक करें, या कोई वाक्य चुनें और टूलबार के AI बटन (Ctrl+J) का उपयोग करें, फिर उत्तर स्वीकार करें या छोड़ दें।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const answers = {
rewrite: 'Our team shipped version 2.4 on time, and the first customers like it a lot.',
grammar: 'We released the new version on Friday, and the customers are happy with it.',
translate: 'Wir haben die neue Version am Freitag veröffentlicht, und die Kunden sind zufrieden.',
summarize: 'Version 2.4 was released on Friday; customers are happy.',
continue: ' Next month we will add comments and version history.',
};
async function* stubModel(prompt, ctx) {
const answer = answers[ctx.command] || 'A stub answer to: ' + ctx.instruction;
for (const word of answer.split(/(?<= )/)) {
await new Promise((resolve) => setTimeout(resolve, 60));
if (ctx.signal.aborted) return;
yield word;
}
}
const editor = VibeFoxJSEditor.createEditor('#app', {
data: '<p>we released the new version on friday and the customers is happy with it.</p><p>The release took three weeks.</p>',
minHeight: '220px',
ai: {
stream: stubModel,
commands: ['rewrite', 'grammar', 'translate', 'summarize', 'continue', { id: 'title', label: 'Suggest a title', prompt: 'Suggest a title for this text.', insert: 'after' }],
languages: ['en', 'de', 'fr'],
},
});
addButtons([
['Summarize', async () => {
const p = editor.content.querySelector('p');
getSelection().setBaseAndExtent(p, 0, p, p.childNodes.length);
const r = await editor.runAI('summarize');
console.log(r.ok, r.text);
}],
['Fix grammar and accept', async () => {
const p = editor.content.querySelector('p');
getSelection().setBaseAndExtent(p, 0, p, p.childNodes.length);
await editor.runAI('grammar', { apply: true });
}],
['Open the AI panel', () => { editor.focus(); editor.openAIPanel(); }],
]);एक्सेसिबिलिटी चेकर
पैनल दस्तावेज़ की समस्याएँ सूचीबद्ध करता है: बिना alt टेक्स्ट की छवि, छूटा हुआ हेडिंग-स्तर, अस्पष्ट लिंक। किसी समस्या पर जाने के लिए उस पर क्लिक करें, या उसका त्वरित समाधान उपयोग करें।
const editor = VibeFoxJSEditor.createEditor('#app', { minHeight: '300px' });
editor.setData('<h1>Report</h1><h3>Sales</h3><p><img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==" width="80" height="40"></p>' +
'<p>For the details <a href="/report">click here</a>.</p><p style="color: #bbbbbb">Light gray text is hard to read.</p>');
const { issues, counts } = editor.checkAccessibility({ level: 'AA' });
console.log(counts, issues.map((i) => i.rule));
editor.toggleAccessibilityPanel(true);अपने ब्लॉक और इनलाइन नोड
पेज द्वारा रजिस्टर किए गए "callout" ब्लॉक और "kbd" इनलाइन नोड, उनके टूलबार बटनों और स्लैश एंट्री के साथ। किसी पैराग्राफ़ में क्लिक करें और Callout बटन उपयोग करें, या /callout टाइप करें।
VibeFoxJSEditor.registerBlock({
name: 'callout',
className: 'callout',
content: 'blocks',
attrs: { kind: { default: 'info', values: ['info', 'warning'] } },
label: { en: 'Callout', de: 'Hinweis' },
icon: 'quote',
toolbar: ['callout'],
slash: ['callout'],
keys: { 'Mod+Alt+C': 'callout' },
markdown: (el, { content }) => '> **Note:** ' + content(),
});
VibeFoxJSEditor.registerInline({ name: 'kbd', tag: 'kbd', content: 'text', label: 'Key', toolbar: ['kbd'] });
const style = document.createElement('style');
style.textContent = '.callout { border-left: 4px solid #2563eb; background: #eff6ff; padding: 4px 12px; margin: 8px 0; }' +
'.callout[data-kind="warning"] { border-color: #d97706; background: #fffbeb; }' +
'kbd { border: 1px solid #94a3b8; border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; font: 0.9em ui-monospace, monospace; }';
document.head.append(style);
const doc = VibeFoxJSEditor.createEditor('#app', {
data: '<p>Press <kbd data-vfjs-inline="kbd">Ctrl</kbd> + <kbd data-vfjs-inline="kbd">S</kbd> to save.</p><p>Back up your files first.</p>',
toolbar: [['bold', 'italic'], ['callout', 'kbd']],
});
const p = doc.content.querySelectorAll('p')[1];
getSelection().setBaseAndExtent(p.firstChild, 0, p.firstChild, 0);
doc.focus(); doc.exec('callout', { kind: 'warning' });
console.log(VibeFoxJSEditor.getBlockTypes().map((t) => t.name));थीम और स्टाइलिंग
थीम
सूची एडिटर की थीम बदलती है: अंतर्निहित लाइट, डार्क और हाई-कॉन्ट्रास्ट थीम, और vibefox-js-themes.css की थीम, साथ ही आपकी अपनी थीम "brand"।
function addSelect(label, values, onChange) {
const select = document.createElement('select');
select.setAttribute('aria-label', label);
for (const value of values) select.append(new Option(label + ': ' + value, value));
select.onchange = () => onChange(select.value);
document.getElementById('app').before(select);
return select;
}
const doc = '<h2>Release notes</h2>' +
'<p>Version <b>2.4</b> brings <i>faster search</i>, a new <a href="https://example.com">export</a> and many small fixes.</p>' +
'<ul><li>Search is three times faster</li><li>Export to PDF and Word</li><li>Dark theme</li></ul>' +
'<blockquote><p>The best release so far. — Ann, product manager</p></blockquote>';
VibeFoxJSEditor.registerTheme('brand', { accent: '#e11d48', 'accent-fg': '#ffffff', radius: '10px' });
const editor = VibeFoxJSEditor.createEditor('#app', { data: doc, theme: 'dark' });
const themes = ['dark', 'light', 'high-contrast', 'high-contrast-light', 'paper', 'paper-dark', 'glass', 'glass-dark', 'classic', 'classic-dark',
'ocean', 'ocean-dark', 'forest', 'forest-dark', 'sunset', 'sunset-dark', 'graphite', 'graphite-dark', 'brand'];
addSelect('Theme', themes, (name) => editor.setTheme(name));CSS वेरिएबल
एक नियम सभी VibeFox JS कॉम्पोनेंट्स के लिए (ऐक्सेंट और radius), और एक केवल इस एडिटर के लिए (टूलबार का बैकग्राउंड, पैडिंग और टेक्स्ट का फ़ॉन्ट-साइज़)।
const doc = '<h2>Release notes</h2>' +
'<p>Version <b>2.4</b> brings <i>faster search</i>, a new <a href="https://example.com">export</a> and many small fixes.</p>' +
'<ul><li>Search is three times faster</li><li>Export to PDF and Word</li><li>Dark theme</li></ul>' +
'<blockquote><p>The best release so far. — Ann, product manager</p></blockquote>';
const css = document.createElement('style');
css.textContent = '.vfjs { --vfjs-accent: #0f766e; --vfjs-radius: 4px; }' +
'#notes { --vfjs-editor-toolbar-bg: #f0fdfa; --vfjs-editor-content-padding: 24px; --vfjs-editor-font-size: 17px; --vfjs-editor-heading-fg: #0f766e; }';
document.head.append(css);
document.getElementById('app').innerHTML = '<div id="notes"></div>';
const editor = VibeFoxJSEditor.createEditor('#notes', { data: doc, toolbar: 'basic' });तैयार स्टाइल
सूची तैयार स्टाइल बदलती है: क्लासिक, मिनिमल और फ़्लोटिंग।
function addSelect(label, values, onChange) {
const select = document.createElement('select');
select.setAttribute('aria-label', label);
for (const value of values) select.append(new Option(label + ': ' + value, value));
select.onchange = () => onChange(select.value);
document.getElementById('app').before(select);
return select;
}
const doc = '<h2>Release notes</h2>' +
'<p>Version <b>2.4</b> brings <i>faster search</i>, a new <a href="https://example.com">export</a> and many small fixes.</p>' +
'<ul><li>Search is three times faster</li><li>Export to PDF and Word</li><li>Dark theme</li></ul>' +
'<blockquote><p>The best release so far. — Ann, product manager</p></blockquote>';
const editor = VibeFoxJSEditor.createEditor('#app', { data: doc, style: 'floating' });
addSelect('Style', ['floating', 'classic', 'minimal'], (name) => editor.setStyle(name));
editor.on('styleChange', (e) => console.log(e.style));घनत्व और आकार
सूचियाँ कंटेनर पर data-vfjs-density और स्केल --vfjs-size-unit सेट करती हैं: टूलबार के बटन और पैडिंग उनके अनुसार बदलते हैं।
function addSelect(label, values, onChange) {
const select = document.createElement('select');
select.setAttribute('aria-label', label);
for (const value of values) select.append(new Option(label + ': ' + value, value));
select.onchange = () => onChange(select.value);
document.getElementById('app').before(select);
return select;
}
const doc = '<h2>Release notes</h2>' +
'<p>Version <b>2.4</b> brings <i>faster search</i>, a new <a href="https://example.com">export</a> and many small fixes.</p>' +
'<ul><li>Search is three times faster</li><li>Export to PDF and Word</li><li>Dark theme</li></ul>' +
'<blockquote><p>The best release so far. — Ann, product manager</p></blockquote>';
const app = document.getElementById('app');
const editor = VibeFoxJSEditor.createEditor('#app', { data: doc, toolbar: 'basic' });
app.dataset.vfjsDensity = 'compact';
addSelect('Density', ['compact', 'normal', 'comfortable'], (value) => { app.dataset.vfjsDensity = value; });
addSelect('Size unit', ['1px', '1.25px', '0.85px'], (value) => app.style.setProperty('--vfjs-size-unit', value));आपका अपना CSS
पेज के सादे CSS नियम सक्रिय टूलबार बटनों और हेडिंग को बदलते हैं, !important के बिना। सक्रिय बटन देखने के लिए कैरेट को बोल्ड शब्द में रखें।
const doc = '<h2>Release notes</h2>' +
'<p>Version <b>2.4</b> brings <i>faster search</i>, a new <a href="https://example.com">export</a> and many small fixes.</p>' +
'<ul><li>Search is three times faster</li><li>Export to PDF and Word</li><li>Dark theme</li></ul>' +
'<blockquote><p>The best release so far. — Ann, product manager</p></blockquote>';
const css = document.createElement('style');
css.textContent = '.vfjs-editor-toolbar .vfjs-editor-btn.vfjs-editor-is-active { color: #fff; background: #1d4ed8; }' +
'.vfjs-editor-content h2 { letter-spacing: -0.01em; border-bottom: 2px solid #1d4ed8; }' +
'.vfjs-editor-content blockquote { font-style: italic; }';
document.head.append(css);
const editor = VibeFoxJSEditor.createEditor('#app', { data: doc, toolbar: 'basic' });
editor.focus();
const b = editor.content.querySelector('b');
getSelection().setBaseAndExtent(b.firstChild, 1, b.firstChild, 1);स्थानीयकरण और RTL
भाषाएँ
रूसी भाषा में एडिटर, जिसमें लाइब्रेरी के बाद लोड किया गया भाषा पैक vibefox-js-editor.ru.js है: टूलटिप, मेनू, प्लेसहोल्डर और स्टेटस बार। सूची इस एडिटर की भाषा बदलती है।
function addSelect(label, values, onChange) {
const select = document.createElement('select');
select.setAttribute('aria-label', label);
for (const value of values) select.append(new Option(label + ': ' + value, value));
select.onchange = () => onChange(select.value);
document.getElementById('app').before(select);
return select;
}
const editor = VibeFoxJSEditor.createEditor('#app', { data: '<p>Текст документа не переводится.</p><p><br></p>', locale: 'ru' });
addSelect('Locale', ['ru', 'en'], (lang) => editor.setOptions({ locale: lang }));नई भाषा जोड़ना
एक आंशिक जर्मन शब्दकोश: प्लेसहोल्डर, स्टेटस बार और कुछ टूलटिप जर्मन में हैं, और जो कुंजियाँ छूटी हैं, उनके लिए अंग्रेज़ी का उपयोग होता है।
VibeFoxJSEditor.addLocale('de', {
'editor.placeholder': 'Schreiben Sie, oder drücken Sie „/“ für Blöcke…',
'editor.bold': 'Fett',
'editor.italic': 'Kursiv',
'editor.underline': 'Unterstrichen',
'editor.undo': 'Rückgängig',
'editor.redo': 'Wiederholen',
'editor.words': 'Wörter',
'editor.chars': 'Zeichen',
});
const editor = VibeFoxJSEditor.createEditor('#app', { locale: 'de', toolbar: 'basic' });राइट-टू-लेफ़्ट
राइट-टू-लेफ़्ट दस्तावेज़: टूलबार, सूचियाँ और उद्धरण मिरर किए गए हैं; टूलबार में बाएँ-से-दाएँ और दाएँ-से-बाएँ पैराग्राफ़ के बटन हैं। आख़िरी पैराग्राफ़ बाएँ-से-दाएँ है।
const editor = VibeFoxJSEditor.createEditor('#app', {
dir: 'rtl',
data: '<h2>מסמך לדוגמה</h2><p>זהו טקסט בעברית שנכתב מימין לשמאל.</p><ul><li>פריט ראשון</li><li>פריט שני</li></ul>' +
'<blockquote><p>ציטוט קצר.</p></blockquote><p dir="ltr">This paragraph is left-to-right.</p>',
});
editor.on('directionChange', (e) => console.log(e.dir));अन्य VibeFox JS लाइब्रेरी के साथ उपयोग
link() से कॉम्पोनेंट्स को जोड़ना
एडिटर की गिनती link() के ज़रिए एक VibeFox JS Grid तालिका में जाती है (दोनों लाइब्रेरी इस पेज पर हैं)। एडिटर में टाइप करें और तालिका देखें।
const app = document.getElementById('app');
app.innerHTML = '<div id="doc"></div><div id="stats" style="margin-top: 8px"></div>';
const editor = VibeFoxJSEditor.createEditor('#doc', { data: '<p>Type here: the table below counts the words.</p>', toolbar: 'basic', minHeight: '100px' });
const grid = VibeFoxJSGrid.createGrid('#stats', {
height: 140,
columns: [{ field: 'name', header: 'Count' }, { field: 'value', header: 'Value', type: 'number' }],
data: [],
});
const rows = (e) => [{ name: 'Words', value: e.words }, { name: 'Characters', value: e.chars }, { name: 'Reading time, min', value: e.readingTime }];
const unlink = VibeFoxJSEditor.link(editor, 'wordCountChange', grid, 'setData', rows);
grid.setData(rows({ ...editor.getWordCount() }));AI टूल
बटन एडिटर के टूल को वैसे कॉल करते हैं जैसे कोई मॉडल करेगा: सामग्री को Markdown के रूप में पढ़ना, एक शब्द बदलना, और एक स्टब मॉडल के साथ kit.ask() चलाना, जो एक टूल-कॉल माँगता है। परिणाम एडिटर के नीचे दिखाए जाते हैं।
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
const output = document.createElement('pre');
output.style.cssText = 'height: 100px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const { describeTools, callTool, createToolKit } = VibeFoxJSEditor;
const editor = VibeFoxJSEditor.createEditor('#app', { data: '<h2>Plan</h2><p>Ship the release on Friday.</p>', toolbar: 'basic', minHeight: '120px' });
const show = (x) => { output.textContent = JSON.stringify(x, null, 1); };
// stub model: first asks for a tool call, then answers with a text (a real one is your request to a model)
let step = 0;
const stubModel = async (request) => (++step % 2
? { text: '', toolCalls: [{ id: 'c1', name: 'doc_editor_insert', arguments: { content: 'Summary: the release is on Friday.', position: 'end' } }] }
: { text: 'Added a summary at the end.' });
addButtons([
['Tool names', () => show(describeTools(editor, { format: 'openai' }).map((t) => t.function.name))],
['Get content', async () => show(await callTool(editor, 'editor_get_content', { format: 'markdown' }))],
['Replace "Friday"', async () => show(await callTool(editor, 'editor_find_replace', { query: 'Friday', replacement: 'Monday' }))],
['Ask (stub model)', async () => {
const kit = createToolKit({ doc: editor });
const answer = await kit.ask('Add a short summary at the end', { model: stubModel });
show({ text: answer.text, calls: answer.calls.map((c) => c.name) });
}],
]);