Skip to main content

Crate iced_markdown_editor

Crate iced_markdown_editor 

Source
Expand description

§iced-markdown-editor

A Word-style WYSIWYG CommonMark editor widget for Iced, with the GFM extensions strikethrough, task lists and tables. Desktop and web.

ⓘ
use iced_markdown_editor::{markdown_editor, Action, EditorState, Event};

struct App { editor: EditorState }

#[derive(Debug, Clone)]
enum Message { Editor(Action) }

impl App {
    fn update(&mut self, message: Message) -> iced::Task<Message> {
        match message {
            Message::Editor(action) => {
                let output = self.editor.update(action);
                if output.events.contains(&Event::Changed) {
                    println!("{}", self.editor.markdown());
                }
                output.task.map(Message::Editor)
            }
        }
    }

    fn view(&self) -> iced::Element<'_, Message> {
        markdown_editor(&self.editor).view().map(Message::Editor)
    }

    fn subscription(&self) -> iced::Subscription<Message> {
        self.editor.subscription().map(Message::Editor)
    }
}

§Features

FeatureDefaultAdds
syntectoffSyntectHighlighter, a ready-made code highlighter
imagesoffShowing images, and pasting images from the clipboard (desktop)

§Tables

GFM tables are edited in place: Command::InsertTable, row and column commands, Command::SetColumnAlign with ColumnAlign. Tab moves to the next cell, Enter to the cell below.

§Switching features off

Tables, task lists and strikethrough can be disabled at runtime with Features (EditorState::features).

§Theme

Colours and font sizes come from an EditorStyle: a preset, one derived from the application’s Iced theme, or your own Palette and Typography (EditorState::style).

use iced_markdown_editor::{EditorState, EditorStyle};

let editor = EditorState::new().style(EditorStyle::dark());

// Match the rest of the application.
let editor = EditorState::new().style(EditorStyle::from_theme(&iced::Theme::Dark));

EditorState::set_style changes it at runtime, e.g. when the application theme changes.

§Languages

The toolbar, link bar and status bar speak English, German, French, Spanish and Italian (Language, EditorState::language). The document itself is never translated.

use iced_markdown_editor::{EditorState, Language};

// E.g. from the operating system locale; unknown tags fall back to English.
let language = Language::from_tag("de-CH").unwrap_or_default();
let editor = EditorState::new().language(language);

EditorState::set_language switches it at runtime.

§Extensions

A BlockRenderer (from your own crate) draws fenced code blocks of chosen languages, e.g. formulas or diagrams (EditorState::block_renderer). A CodeHighlighter colours code.

Re-exports§

pub use extension::BlockRenderer;
pub use extension::RenderRequest;
pub use features::Features;
pub use highlight::CodeHighlighter;
pub use highlight::HighlightRequest;
pub use highlight::HighlightSpan;
pub use i18n::Language;
pub use images::ImageId;
pub use images::ImageLoader;
pub use images::ImageMode;
pub use images::ImageRejection;
pub use images::ImageSettings;
pub use images::ImageSource;
pub use images::PastedImage;
pub use page::IsoSeries;
pub use page::Margins;
pub use page::Measure;
pub use page::Orientation;
pub use page::PageSize;
pub use page::Paper;
pub use page::ParseError;
pub use page::Unit;
pub use style::EditorStyle;
pub use style::Palette;
pub use style::Typography;
pub use widget::Action;
pub use widget::Command;
pub use widget::DEFAULT_TAB_WIDTH;
pub use widget::EditorState;
pub use widget::Event;
pub use widget::Formatting;
pub use widget::MarkdownEditor;
pub use widget::Mode;
pub use widget::Output;
pub use widget::markdown_editor;
pub use zoom::KeyBinding;
pub use zoom::ZoomSettings;
pub use zoom::ZoomShortcuts;
pub use crate::syntect::SyntectHighlighter;syntect

Modules§

extension
Custom rendering of fenced code blocks.
features
Optional Markdown features, switched on or off at runtime.
highlight
Code highlighting plugin interface.
i18n
Interface language of the built-in toolbar, link bar and status bar.
images
Images: paste, drop and insert; embed in the Markdown or hand them to the application; display.
page
Paper setup: page size, orientation and margins.
style
Theming.
syntectsyntect
Code highlighter based on syntect (Sublime Text grammars). Available with the syntect feature.
widget
The public widget API.
zoom
Zoom settings and key bindings.

Enums§

BlockKind
Paragraph-level formatting as offered in the toolbar and the API.
ColumnAlign
Horizontal alignment of a table column (GFM).

Constants§

FONTS
All bundled font files. Register them with your application:

Functions§

normalize
Parses and writes Markdown again, in the form the editor saves it.