Put it in your app Without a framework
Without a framework
@rebodocs/editor is plain TypeScript and the DOM. The Editor class is the pages; everything around them is a piece of its own, made if you want it, in the element you give it.
Put it together
One Editor, made once, and the pieces around it: each choice destroys them all and makes the chosen ones again, wired to each other. The document and its history stay.
Opening the editor…
The smallest page
<div id="toolbar"></div>
<div id="pages" style="height: 80vh"></div>
<div id="status"></div>import { Editor, createToolbar, createStatusBar, createBubble } from '@rebodocs/editor';
const editor = new Editor(document.querySelector('#pages')!);
createToolbar(document.querySelector('#toolbar')!, editor);
createStatusBar(document.querySelector('#status')!, editor);
createBubble(editor);
editor.loadDocx(bytes);
editor.onChange(() => console.log(editor.pageCount, 'pages'));
const docx = editor.exportDocx();
const pdf = await editor.exportPdf('Contract');The pieces
Each is a function that takes the element it goes in, when it has one, and gives back what it made, with destroy().
- new Editor(element)
- The pages. Holds the document, the selection and the history; the element needs a height.
- createToolbar(el, editor, options)
- One row of formatting; what does not fit folds into a tray. Its options give it the buttons that open the other pieces.
- createRuler(el, editor)
- Margins, indents and tab stops, to drag or to move with the keys.
- createPanel(el, editor, { versions?, rail?, onError?, onToggle? })
- The side panel: outline, template variables, notes, tracked changes, and versions when the application keeps them. show(tab?), hide(), toggle() and isOpen; onToggle is called when it opens or closes, by its own buttons too, and rail sets its tabs as icons always there.
- createStatusBar(el, editor, options)
- Pages, words, the language of the text, the view, the zoom; who is on the document and the assistant, when given.
- createBubble(editor, { forms? })
- The bar beside the selection, with what can be done to it; on a phone, copy and paste too. With forms, making the selection an area to fill in.
- createSlashMenu(editor, { ask?, forms?, builtIns?, commands? })
- The menu a “/” brings up at the start of a paragraph or after a space: headings, lists, a table, a picture, the signatures, and your own commands. See Headless, The “/” menu.
- createVariableSuggest(editor, { variables? })
- The list “{{” brings up: the variables you fill, then those of the document, written whole. See Templates, In the editor.
- createFindBar(editor)
- Find and replace, opened with open() or ⌘F (Ctrl+F).
- createSidebar(editor, options)
- The document menu: open, save, export, page setup, letterhead, the language of the interface. A card from the button of the menu, or with variant: 'screen' a screen of its own. With within: an element, it stays inside that box instead of the whole window.
- createFocusMode(editor, options)
- The pages alone, full screen, with ⌘⇧. (Ctrl+Shift+.); Escape leaves.
- createResponsive(shell, editor)
- Narrow windows and the keyboard of a phone. See Phones and touch.
And for what an interface also needs:
- createVersionView(editor, versions)
- A version shown beside the document, its changes marked.
- createImagePicker(editor) · openImageGrid(editor, files)
- Asking for pictures, and the dialog that sets out several in a grid.
- setAuthor(editor, name) · recallAuthor(editor)
- The name notes and changes are signed with, kept for the next visit.
- addFont(source) · setLocale(code) · addLocale(code, texts)
- Fonts of your own, and the language of the interface. See Fonts and Languages.
- createSpellChecker({ dictionaries }) → editor.spellChecker
- The red wave under words spelled wrong, with Hunspell dictionaries you give, read in a worker. See Spelling.
- applyTheme(element, theme) · editor.theme
- The dark theme, or 'auto' for the system's: on the element around the pieces, and on the editor for what it shows outside it.
Wiring them together
A piece does not look for the others: the options of one take the methods of another. The toolbar shows the button of the menu, the panel or the search only when it is given what to open.
- toolbar onMenu: menu.toggle
- The button at its start opens the document menu.
- toolbar onPanel: panel.toggle · panelOpen: () => panel.isOpen
- The button that shows and hides the side panel, at the end of the bar, pressed while it is open; panelSide: 'left' puts it at the start, for a panel on the left.
- panel onToggle: () => toolbar.refresh()
- The button is pressed again when the panel is closed with its own button.
- toolbar onFind: () => find.open()
- The search button at its end.
- toolbar onInsertLink · onInsertNote · onInsertVariable
- Insert → Link, Note and Variable ask in the selection bar: () => bubble.ask('link'). Without them they ask in a prompt of the browser.
- createSlashMenu(editor, { ask: bubble.ask })
- The “/” menu offers a variable and a note, asked for in the selection bar; without ask, it leaves them out.
- editor.onLinkKey = () => bubble.ask('link')
- ⌘K (Ctrl+K) asks for the address there too; without it, the key is left to the browser.
- status focus: { on: () => focus.on, toggle: focus.toggle }
- The full-screen button at the end of the status bar.
The code under the demo above is exactly this wiring, for the pieces chosen. A page that wires every one of them, with collaboration and an assistant as well, is apps/playground/src/main.ts in the repository.
Changing the language
The pieces take their words when they are made. To change the language with everything open, destroy them and make them again; the editor keeps the document, the selection and the history.
import { onLocaleChange, setLocale } from '@rebodocs/editor';
let pieces = makePieces(editor); // your function: creates them, returns them
onLocaleChange(() => {
pieces.forEach((piece) => piece.destroy());
pieces = makePieces(editor); // made again, in the new language
});
setLocale('es');The editor itself follows the language by itself: what it says to a screen reader, and the names of the styles. See Languages.
The whole thing at once
mountDocs builds all of that in any element, wired, with the options of the React component, and changes the language by itself.
import { mountDocs } from '@rebodocs/react/mount';
const docs = mountDocs(document.querySelector('#editor')!, {
docx: bytes,
name: 'contract.docx',
locale: 'en',
onSave: (docx, name) => upload(docx, name),
});
docs.editor; // the Editor, for everything else
docs.destroy();It runs no React: it is the /mount entry of @rebodocs/react, which holds none, and React is an optional peer of the package, so npm does not install it. Only the components and hooks of the main entry need it.
Working with the editor
Everything the pieces do, they do through its methods, and so can you. All of them are on the page Editor API; a few to start with:
editor.setSelection({ para: 0, offset: 0 }, { para: 0, offset: 5 });
editor.toggleMark('bold');
editor.insertText('Hello');
editor.undo();
editor.setZoom(1.25);