Repository navigation
Rich Text Editor
The rich text editor stores a document with structure. Users can select, format and edit the text with the pointer and the keyboard. Create the shared document one time, outside the UI builder. Then the edits stay after a resize or a theme change.
use blinc::layout::prelude::*;
use blinc::cn::components::rich_text_editor::{
RichDocument, RichTextTheme, rich_text_editor, rich_text_state,
};
use blinc::core::Color;
let document = rich_text_state(RichDocument::from_markdown(
"## Your ideas\n\nWrite **something meaningful**.", Color::TRANSPARENT,
));
let editor = rich_text_editor(&document, RichTextTheme::default(), 380.0);The editor wraps its text to its width, so the text follows the width of the parent. The last
argument is the width that the editor uses before the first layout. RichTextTheme::padding sets the
space around the text:
let editor = rich_text_editor(&document, RichTextTheme::default().padding(16.0), 380.0);Text with a transparent color takes the CSS text color of the editor. If a part of the text has its own color, that color stays when the theme changes.
You can select text with the pointer, move with the arrow keys and type. Enter, Backspace and the usual editing shortcuts of your system also work. To use the format shortcuts, click the editor first. The shared document holds the content. If your app must react to edits, read the changes of the document. Do not build the editor again for each key.
The toolbar uses cn components. It has a button with a tooltip for bold, italic, underline, strikethrough and code. It also has the cn color picker and a link button.
The link button opens a small window with two fields. The Text field holds the selected text. The Link field holds the address.
- Type the address in the Link field.
- Press Enter, or click Apply. The editor replaces the selected text with the text of the Text field and links it.
If you select no text, Apply inserts the text at the cursor, with the link. To remove a link, empty the Link field and apply.
RichTextTheme::toolbar sets the place of the toolbar:
use blinc::cn::components::rich_text_editor::ToolbarPlacement;
// Over the selected text, with an arrow that points to it. This is the default.
let floating = RichTextTheme::default().toolbar(ToolbarPlacement::Floating);
// Always above the text, with a menu for the paragraph or heading level.
let fixed = RichTextTheme::default().toolbar(ToolbarPlacement::Fixed);
// No toolbar. Use shortcuts or your own controls to format.
let hidden = RichTextTheme::default().toolbar(ToolbarPlacement::Hidden);If you select no text, a format button applies to the text that you type next. The floating toolbar shows after you release the pointer from a selection. Its close button removes the selection. The fixed toolbar has a menu for the paragraph or heading level.
If the editor is narrower than the toolbar, the controls scroll between two buttons. If there are more controls past an edge, that edge fades. The heading menu of the floating toolbar shows short names: P and H1 to H6. The fixed toolbar shows the full names.
You can change the style of both toolbars with CSS:
- The floating toolbar uses
.cn-rich-text-editor-floating-toolbar. Itsbackground,border,border-radiusandbox-shadowalso apply to its point. - The fixed toolbar uses
.cn-rich-text-editor-fixed-toolbar. - A pressed button in both toolbars has
.cn-rich-text-editor-button--active.
A block can be an item of a bulleted list, a numbered list or a checklist. A block is one part of the document, for example a paragraph. To change the list type, use the list menu of the toolbar or these shortcuts. On systems other than macOS, use Ctrl instead of Cmd.
| Keys | List |
|---|---|
| Cmd+Shift+8 | Bulleted list |
| Cmd+Shift+7 | Numbered list |
| Cmd+Shift+6 | Checklist |
- Tab moves an item one level in, and Shift+Tab moves it one level out.
- Enter starts a new item.
- Enter on an empty item moves it one level out. At the first level, it leaves the list.
- Backspace at the start of an item also moves it one level out.
- To tick a checklist item, click its box or press Cmd+Enter.
A quote is not a separate type of block. A paragraph or a heading can also be a quote. Then the menu shows "Heading 2", for example, and the quote button looks pressed.
The quote button makes the selected blocks into quotes. If all of them are quotes, the button removes the quote. If a list item becomes a quote, it changes to a quoted paragraph. Cmd+Shift+9 does the same. In a table, the table menu takes the place of the quote button.
In your code, Block::quote marks a quote. Block::quotable returns true for a block that can be a quote. block_ops::toggle_quote
changes a range of blocks. Backspace at the start of a quote removes the
quote first. In Markdown, a quote is > before a paragraph or a heading.
The table button of the toolbar opens a grid. Choose the size of the new table in the grid. The first row is the header, and the columns share the width of the table. A cell holds one line of text. The line wraps, and it can have all the text formats.
| Keys | What they do |
|---|---|
| Tab | Go to the next cell. In the last cell, add a row |
| Shift+Tab | Go to the cell before |
| Enter | Go to the cell below. In the last row, go to the text after the table |
| Arrow keys | Move through the lines of a cell and between rows |
If the cursor is in a table, a table menu takes the place of the list menu. It adds and removes rows and columns, and removes the table. The alignment buttons align the column of the cursor.
In your code, Block::table(rows, columns) makes a table. The table module changes a table in a
document:
use blinc::cn::components::rich_text_editor::table;
table::insert_table(&mut doc, cursor, 3, 2);A table is one block, BlockKind::Table { columns }. Its lines are the cells, row by row.
columns holds the alignment of each column.
Each block has an align value: BlockAlign::Start, Center, End or Justify. To align the
selected blocks, or the block of the cursor, use the alignment buttons of the toolbar. You can also
use Cmd+Shift+L (start), E (center), R (end) or J (justify). A new paragraph takes the alignment of
the paragraph that you split.
In a table, these controls align the column. A column cannot use Justify. In your code,
block_ops::set_block_align sets the alignment in a document.
RichDocument::from_markdown reads Markdown, and to_markdown writes it. If you write a document and
read it again, you get the same document. There is one exception: a format on a space at the start or
end of a formatted part is lost.
let markdown = document.lock().unwrap().document.to_markdown(Color::TRANSPARENT);
let same = RichDocument::from_markdown(&markdown, Color::TRANSPARENT);Give the same color to both functions. This is the color of the text that has no color of its own,
and to_markdown does not write it.
If Markdown has no syntax for a format, to_markdown writes inline HTML, and from_markdown reads
it:
-
<u>for underline -
<span style="color:#rrggbb">for a color -
<br>for an empty paragraph -
<div align="center">around aligned blocks - HTML tags for emphasis next to punctuation, where Markdown marks cannot work
The editor writes tables as GitHub Flavored Markdown (GFM) tables.
on_change calls your function with the document after an edit in the editor changes it. Typing,
format changes, paste, undo and redo call it. Cursor moves, selections and focus changes do not call
it. Your function can set signals and lock the state:
use blinc::cn::components::rich_text_editor::RichTextStateExt;
document.on_change(move |doc| characters.set(doc.plain_text().chars().count()));suggestion(..) offers text for the user to add at the cursor. It is a method of RichTextStateExt,
like on_change, because the editor has no builder. It takes a string, a signal, or a computed. The
text starts where the cursor is when the suggestion is set. For a computed, that is each time its value
changes. While the cursor is at the end of the line where the suggestion started, the editor shows the
first line of the text after the cursor in a muted color, in the font of the text before the cursor.
If the text has more lines, an ellipsis follows. The muted text is not part of the document, the
selection, or the clipboard.
Typing the start of the text makes it shorter. Any other edit, and any move of the cursor, hides it. It does not show while the editor is empty, has a selection, or does not have focus.
Tab, the right arrow, and End add all of the text at the cursor, in one undo step. A new line in the
text becomes a soft break, which is a new line inside the same paragraph or list item. The added text gets the format
that the next typed character gets. suggestion_keys(..), also a method of RichTextStateExt, takes a list of
blinc::layout::widgets::suggestion::SuggestionKey values: Tab, Right, End and Enter. Enter is
not a default key. If you list Enter, it accepts the text instead of starting a new paragraph, but only
while a suggestion shows. While a suggestion shows and Tab is one of the keys, Tab does not indent or
move between table cells. With no suggestion, Tab indents or moves between table cells.
on_change gives your code the document but not the cursor, so compute the suggestion from the
document. document is the state of the editor, as in the example under Changes. This example
continues a sentence that ends with the quick:
use blinc::cn::components::rich_text_editor::RichTextStateExt;
use blinc::prelude::{computed, signal};
let typed = signal(String::new());
let continuation = computed(move |g| {
if g.get(typed).unwrap_or_default().ends_with("the quick") {
" brown fox".to_string()
} else {
String::new()
}
});
document.on_change(move |doc| typed.set(doc.plain_text()));
document.suggestion(continuation);The muted text has the class blinc-input-suggestion. Its color is var(--text-tertiary), and a rule
for the class changes it.
To change the style of the text area, write CSS rules for .cn-rich-text-editor-document in your
app:
.cn-rich-text-editor-document {
color: var(--text-primary);
background: var(--surface-elevated);
border: 1px solid var(--border);
border-radius: var(--radius-lg);
}Getting Started
Migrating
Mobile Development
Example Gallery
- Example Gallery
- Canvas Element
- Canvas Kit Interactive
- Carousel Demo - Selector API Showcase
- Chrome-Style Tabs
- blinc_cn Components
- Run with
cargo run -p blinc_app_examples --features cn --example cn_pickers_demo. - Code Element
- Complex SVG
- CSS Debug
- CSS Visual Features
- Layer Effects
- Emoji and HTML Entities
- @flow Shader
- Fluid Surface
- Skeleton animation with glTF +
blinc_canvas_kit. - Gpu Pass Demo
- Image CSS Styling
- Image Layer Test
- Keyframe Animation Canvas
- Markdown Editor
- 3D Mesh Demo — renders the Khronos glTF
DamagedHelmetsample model - Motion Demo
- Music Player Glass Card
- Node-editor demo — pre-wired graph with three node types, typed
- Notch Menu Bar
- Overflow Fade
- Overlay System
- Pointer Query
- Rich Text Element
- Rich Text Editor
- Scroll Container
- Semantic @flow
- Sortable
- Stateful API + Signal-bound modifiers demo.
- End-to-end 3D demo wiring Blinc's SceneKit3D renderer up to
- Unified Styling API
- SVG Animation
- Table Builder
- Tabler Icons
- Minimal text positioning test
- Text Input Widgets
- KHR_texture_transform
- Theme System
- Timeline Animation
- Typography
- Wet Glass
- Windowed Application
Web Development
Core Concepts
- Elements & Layout
- Styling & Materials
- CSS Styling
- Theming
- Event Handling
- Accessibility
- State Management
- Lists & Conditional Content
- Element Refs
Animation
Components
Component Library (blinc_cn)
- Overview
- Button
- Card
- Icons
- Typography
- Dialog
- Form Components
- Date, Time and Colour Pickers
- Rich Text Editor
- Code Editor
- Navigation
- Feedback
- Layout
- Data Display
Widgets
- Buttons & Inputs
- Text & Rich Text
- Code Editor
- Scroll Containers
- Virtualized List
- Canvas Drawing
- Gestures
- Notched Surfaces
- Images & SVG
- Audio & Video
- Markdown Rendering
Canvas Kit
Advanced
- Routing & Navigation
- Multi-Window
- System Integration
- Element Query API
- Overlay System
- Custom State Machines
- Pointer Query
- Performance Tips
- Flow Shaders
- 3D Rendering
- Custom GPU Passes
- Hot reloading
Architecture
Contributing