ElyMark: right-click menu by target; Copy as HTML Code and Plain Text

R4.10. Clipboard row, Copy / Paste As, format and block rows, Table or
Paragraph, Insert, link, image, code and raw-block items; source and
preview menus. Component pin 1b5fb07. Records the user's rule that
ElyMark must work, not match Typora one to one.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-14 18:41:24 -04:00
1 parent f012eaa624
commit d5ceaf9aac
18 files changed
+840 -67

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
//! Rich, HTML code and plain text copies (R4.10).
use std::path::PathBuf;
use ely_gpui_component::primitives::Severity;
use ely_office::prelude::*;
use ely_platform::clipboard::{self, Clip};
use ely_ui::i18n::text;
use gpui::{ClipboardItem, Context, Div, InteractiveElement as _, actions};
use crate::desk::Desk;
use crate::output::{self, Pictures};
actions!(mark, [CopyRich, CopyHtml, CopyPlain]);
impl Desk {
pub(super) fn copy_actions(root: Div, cx: &mut Context<Self>) -> Div {
root.on_action(cx.listener(|desk, _: &CopyRich, _, cx| desk.copy_rich(cx)))
.on_action(cx.listener(|desk, _: &CopyHtml, _, cx| desk.copy_html(cx)))
.on_action(cx.listener(|desk, _: &CopyPlain, _, cx| desk.copy_plain(cx)))
}
/// The selection or whole text, and the file's folder.
fn copied_text(&mut self, cx: &mut Context<Self>) -> Option<(String, Option<PathBuf>)> {
let Some(view) = self.active_view() else {
self.no_document(cx);
return None;
};
let document = view.read(cx).document().read(cx);
let selected = document.selected(cx);
let markdown = match selected.is_empty() {
true => document.text(cx),
false => selected,
};
let beside = (self.active_tab())
.and_then(|tab| tab.session.path())
.and_then(|path| path.parent())
.map(PathBuf::from);
Some((markdown, beside))
}
fn copy_failed(&mut self, error: &std::io::Error, cx: &mut Context<Self>) {
let said = text(cx, "mark.copy.failed", &[("error", &error.to_string())]);
self.toast(said, Severity::Danger, cx);
}
/// HTML for pasting, the Markdown beside it.
fn copy_rich(&mut self, cx: &mut Context<Self>) {
let Some((markdown, beside)) = self.copied_text(cx) else {
return;
};
let copied =
output::body(&markdown, beside.as_deref(), &Pictures::Inline).and_then(|mut page| {
let clip = Clip::Html {
html: std::mem::take(&mut page.html),
text: markdown,
};
clipboard::write(&clip)
.map(|()| page)
.map_err(|error| std::io::Error::other(format!("{error:#}")))
});
match copied {
Ok(page) => self.left_out(&page, cx),
Err(error) => self.copy_failed(&error, cx),
}
}
/// The HTML as text; pictures keep their addresses.
fn copy_html(&mut self, cx: &mut Context<Self>) {
let Some((markdown, beside)) = self.copied_text(cx) else {
return;
};
match output::body(&markdown, beside.as_deref(), &Pictures::Written) {
Ok(mut page) => {
log::info!("mark: {} bytes copied as HTML code", page.html.len());
cx.write_to_clipboard(ClipboardItem::new_string(std::mem::take(&mut page.html)));
self.left_out(&page, cx);
}
Err(error) => self.copy_failed(&error, cx),
}
}
fn copy_plain(&mut self, cx: &mut Context<Self>) {
let Some((markdown, _)) = self.copied_text(cx) else {
return;
};
match output::plain(&markdown) {
Ok(plain) => {
log::info!("mark: {} bytes copied as plain text", plain.len());
cx.write_to_clipboard(ClipboardItem::new_string(plain));
}
Err(error) => self.copy_failed(&error, cx),
}
}
}
+8 -40
View File
@@ -9,20 +9,20 @@ use ely_gpui_component::{
theme::ControlSize,
};
use ely_office::prelude::*;
use ely_platform::clipboard::{self, Clip};
use ely_ui::{
commands::{Export, Print},
i18n::text,
};
use gpui::{
AnyElement, Context, Div, InteractiveElement, IntoElement, ParentElement as _, TaskExt as _,
actions,
};
use crate::desk::Desk;
use crate::output::{self, Format, Page, Pictures, Target};
use crate::output::{self, Format, Page, Target};
actions!(mark, [CopyRich]);
mod copy;
pub use copy::{CopyHtml, CopyPlain, CopyRich};
const FORMATS: [(Format, &str, &str); 4] = [
(Format::Page, "page", "html"),
@@ -41,9 +41,10 @@ fn key(format: Format) -> &'static str {
impl Desk {
pub fn output_actions(root: Div, cx: &mut Context<Self>) -> Div {
root.on_action(cx.listener(|desk, _: &Export, _, cx| desk.ask_export(cx)))
.on_action(cx.listener(|desk, _: &Print, _, cx| desk.print(cx)))
.on_action(cx.listener(|desk, _: &CopyRich, _, cx| desk.copy_rich(cx)))
let root = root
.on_action(cx.listener(|desk, _: &Export, _, cx| desk.ask_export(cx)))
.on_action(cx.listener(|desk, _: &Print, _, cx| desk.print(cx)));
Self::copy_actions(root, cx)
}
pub(crate) fn no_document(&self, cx: &mut Context<Self>) {
@@ -275,39 +276,6 @@ impl Desk {
})
.detach_and_log_err(cx);
}
/// The selection, or the whole text, as HTML.
fn copy_rich(&mut self, cx: &mut Context<Self>) {
let Some(view) = self.active_view() else {
return self.no_document(cx);
};
let document = view.read(cx).document().clone();
let selected = document.read(cx).selected(cx);
let markdown = match selected.is_empty() {
true => document.read(cx).text(cx),
false => selected,
};
let beside = self
.active_tab()
.and_then(|tab| tab.session.path())
.and_then(|path| path.parent());
let copied = output::body(&markdown, beside, &Pictures::Inline).and_then(|mut page| {
let clip = Clip::Html {
html: std::mem::take(&mut page.html),
text: markdown,
};
clipboard::write(&clip)
.map(|()| page)
.map_err(|error| std::io::Error::other(format!("{error:#}")))
});
match copied {
Ok(page) => self.left_out(&page, cx),
Err(error) => {
let said = text(cx, "mark.copy.failed", &[("error", &error.to_string())]);
self.toast(said, Severity::Danger, cx);
}
}
}
}
/// A file of its own for each print job.