editor documentation
[?]
Apr 10, 2023, 7:42 AM
H5UZI3YNAPSZIQHLCAU67OKHY2XQJC6MR5AKJNYDKK7L6IZ2IINACDependencies
- [2]
N2NUGNN4include a brief reference enabling many useful apps
Change contents
- edit in reference.md at line 184
### text editor primitives - edit in reference.md at line 187
The text-editor widget includes extremely thorough automated tests to give youearly warning if you break something.* `state = edit.initialize_state(top, left, right, font_height, line_height)` --returns an object that can be used to render an interactive editor widgetsfor text and line drawings starting at `y=top` on the app window, between`x=left` and `x=right`. Wraps long lines at word boundaries where possible,or in the middle of words (no hyphenation yet) when it must.* `edit.quit()` -- calling this ensures any final edits are flushed to diskbefore the app exits.* `edit.draw(state)` -- Call this from `App.draw` to display the currenteditor state on the app window as requested in the call to`edit.initialize_state` that created `state`.* `edit.update(state, dt)` -- call this from `App.update` to periodically autosaves editor contents to disk.* `edit.mouse_press(state, x,y, mouse_button)` and `edit.mouse_release(x,y,mouse_button)` -- call these to position the cursor or select some text.* `edit.mouse_wheel_move(state, dx,dy)` -- call this to scroll the editor inresponse to a mouse wheel.* `edit.keychord_press(state, chord, key)` and `edit.key_release(state, key)`-- call these to perform some standard shortcuts: insert new lines,backspace/delete, zoom in/out font size, cut/copy/paste to and from theclipboard, undo/redo.* `edit.text_input(state, t)` -- call this to insert keystrokes into thebuffer.* `Text.redraw_all(state)` -- call this to clear and recompute any cachedstate as the cursor moves and the buffer scrolls.If you need more precise control, look at the comment at the top of`edit.initialize_state` in edit.lua. In brief, the widget contains an array of`lines`. Positions in the buffer are described in _schema-1_ locationsconsisting of a `line` index and a code-point `pos`. We may also convert themat times to _schema-2_ locations consisting of a `line`, `screen_line` and`pos` that better indicates how long lines wrap. Schema-2 locations are neverpersisted, just generated as needed from schema-1. Important schema-1locations in the widget are `cursor1` describing where text is inserted ordeleted and `screen_top1` which specifies how far down the lines is currentlyvisible on screen.