forked from 1b-agent/opencadstudio-upstream
feat(clipboard): PASTESPEC pastes in a chosen format
PASTESPEC offers the formats on the clipboard: Objects and Block for objects copied in the application (the PASTECLIP and PASTEBLOCK paths), and Text for the system clipboard's text, placed as multi-line text in the current text style with its lines as paragraphs. Refs #645 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
4ffae00199
commit
b1d4c9bd9b
29 files changed
+242
-1
No files matched your search
@@ -95,6 +95,23 @@ impl OpenCADStudio {
|
||||
}
|
||||
}
|
||||
|
||||
/// PASTESPEC with the formats on hand: the copied objects, and `text`
|
||||
/// from the system clipboard once it has been read.
|
||||
pub(in crate::app) fn paste_special_command(
|
||||
&self,
|
||||
i: usize,
|
||||
text: Option<&str>,
|
||||
) -> Box<dyn CadCommand> {
|
||||
let document = &self.tabs[i].scene.document;
|
||||
let defaults = crate::scene::creation_style::current_text_defaults(document);
|
||||
Box::new(crate::modules::draw::clipboard::pastespec::PasteSpecCommand::new(
|
||||
!self.clipboard.is_empty(),
|
||||
text,
|
||||
defaults.style_name,
|
||||
defaults.height,
|
||||
))
|
||||
}
|
||||
|
||||
pub(super) fn dispatch_blocks(&mut self, cmd: &str, i: usize) -> Option<Task<Message>> {
|
||||
match cmd {
|
||||
// ── BASE — drawing insertion base point ───────────────────────
|
||||
@@ -284,6 +301,19 @@ impl OpenCADStudio {
|
||||
}
|
||||
}
|
||||
|
||||
// PASTESPEC — offer the copied objects now; the system clipboard's
|
||||
// text joins the formats once read (Message::PasteSpecial).
|
||||
"PASTESPEC" => {
|
||||
let command = self.paste_special_command(i, None);
|
||||
let started = self.start_count_command(i, command);
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let read = Task::perform(crate::sys::read_clipboard_text(), Message::PasteSpecial);
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let read = iced::clipboard::read_text()
|
||||
.map(|result| Message::PasteSpecial(result.ok().map(|text| (*text).clone())));
|
||||
return Some(Task::batch([started, read]));
|
||||
}
|
||||
|
||||
// PASTEORIG — paste at the entities' original coordinates (no pick).
|
||||
"PASTEORIG" => {
|
||||
if self.clipboard.is_empty() {
|
||||
|
||||
@@ -188,7 +188,7 @@ impl OpenCADStudio {
|
||||
}
|
||||
}
|
||||
|
||||
fn start_count_command(&mut self, i: usize, command: Box<dyn CadCommand>) -> Task<Message> {
|
||||
pub(in crate::app) fn start_count_command(&mut self, i: usize, command: Box<dyn CadCommand>) -> Task<Message> {
|
||||
self.reset_command_start_state(i);
|
||||
self.command_line.push_info(&command.prompt());
|
||||
self.tabs[i].active_cmd = Some(command);
|
||||
|
||||
@@ -2360,6 +2360,8 @@ pub enum Message {
|
||||
PasteShortcut,
|
||||
/// Which text input held focus when `PasteShortcut` arrived.
|
||||
PasteShortcutResolved(PasteFocus),
|
||||
/// Completion of the system text clipboard read PASTESPEC starts.
|
||||
PasteSpecial(Option<String>),
|
||||
/// Completion of a system text clipboard read requested by PASTECLIP.
|
||||
SystemClipboardPaste(SystemClipboardText),
|
||||
/// Web: completion of the clipboard-image read the paste fallback starts
|
||||
|
||||
@@ -6815,6 +6815,32 @@ impl OpenCADStudio {
|
||||
}
|
||||
}
|
||||
|
||||
Message::PasteSpecial(text) => {
|
||||
let i = self.active_tab;
|
||||
let waiting = self.tabs[i]
|
||||
.active_cmd
|
||||
.as_ref()
|
||||
.is_some_and(|command| command.name() == "PASTESPEC");
|
||||
if !waiting {
|
||||
return Task::none();
|
||||
}
|
||||
match text.filter(|text| !text.trim().is_empty()) {
|
||||
Some(text) => {
|
||||
// Same prompt, one more option: swap without echoing it.
|
||||
self.tabs[i].active_cmd = Some(self.paste_special_command(i, Some(&text)));
|
||||
self.push_ucs_to_cmd(i);
|
||||
Task::none()
|
||||
}
|
||||
None if self.clipboard.is_empty() => {
|
||||
self.tabs[i].active_cmd = None;
|
||||
self.command_line
|
||||
.push_error(crate::tr!("clipboard", "no-supported-content").as_ref());
|
||||
Task::none()
|
||||
}
|
||||
None => Task::none(),
|
||||
}
|
||||
}
|
||||
|
||||
Message::SystemClipboardPaste(result) => {
|
||||
use super::SystemClipboardText as Text;
|
||||
|
||||
|
||||
@@ -2380,6 +2380,8 @@ pub(super) fn message_attribute(source: &str) -> Option<(&'static str, &'static
|
||||
"PAPER" => Some(("ui", "paper")),
|
||||
"PAPER: double-click viewport to enter MSPACE\nMODEL: click to switch to Model Space" => Some(("ui", "paper-double-click-viewport-to-enter-mspace-mo")),
|
||||
"PASTEBLOCK: clipboard is empty." => Some(("insert", "pasteblock-clipboard-is-empty")),
|
||||
"PASTESPEC Paste as [Objects/Block/Text]:" => Some(("insert", "pastespec-paste-as")),
|
||||
"PASTESPEC Specify insertion point:" => Some(("insert", "pastespec-specify-insertion-point")),
|
||||
"PASTEBLOCK: {e}" => Some(("insert", "pasteblock-e")),
|
||||
"PASTECLIP Pick insertion point:" => Some(("clipboard", "pasteclip-pick-insertion-point")),
|
||||
"PASTEORIG: clipboard is empty." => Some(("insert", "pasteorig-clipboard-is-empty")),
|
||||
|
||||
@@ -2,3 +2,4 @@ pub mod copy_base;
|
||||
pub mod copy_clip;
|
||||
pub mod cut;
|
||||
pub mod paste;
|
||||
pub mod pastespec;
|
||||
@@ -0,0 +1,136 @@
|
||||
// PASTESPEC — paste the clipboard in a chosen format: the copied objects, the
|
||||
// copied objects as a block, or the system clipboard's text as multi-line text.
|
||||
|
||||
use crate::t;
|
||||
use codec::entities::mtext::AttachmentPoint;
|
||||
use codec::entities::MText;
|
||||
use codec::types::Vector3;
|
||||
use codec::EntityType;
|
||||
use glam::DVec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdOption, CmdResult, WorkingPlane};
|
||||
|
||||
pub struct PasteSpecCommand {
|
||||
/// Objects copied inside the application are waiting on the clipboard.
|
||||
objects: bool,
|
||||
/// The system clipboard's text, already escaped for MTEXT.
|
||||
text: Option<String>,
|
||||
style: String,
|
||||
height: f64,
|
||||
placing_text: bool,
|
||||
plane: WorkingPlane,
|
||||
}
|
||||
|
||||
impl PasteSpecCommand {
|
||||
pub fn new(objects: bool, text: Option<&str>, style: String, height: f64) -> Self {
|
||||
Self {
|
||||
objects,
|
||||
text: text.map(mtext_value),
|
||||
style,
|
||||
height,
|
||||
placing_text: false,
|
||||
plane: WorkingPlane::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Plain text as an MTEXT value: control characters escaped, lines as
|
||||
/// paragraphs.
|
||||
fn mtext_value(text: &str) -> String {
|
||||
text.trim_end_matches(['\r', '\n'])
|
||||
.replace('\\', "\\\\")
|
||||
.replace('{', "\\{")
|
||||
.replace('}', "\\}")
|
||||
.replace("\r\n", "\\P")
|
||||
.replace(['\r', '\n'], "\\P")
|
||||
}
|
||||
|
||||
impl CadCommand for PasteSpecCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"PASTESPEC"
|
||||
}
|
||||
|
||||
fn set_working_plane(&mut self, plane: WorkingPlane) {
|
||||
self.plane = plane;
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
if self.placing_text {
|
||||
t!("PASTESPEC Specify insertion point:").into_owned()
|
||||
} else {
|
||||
t!("PASTESPEC Paste as [Objects/Block/Text]:").into_owned()
|
||||
}
|
||||
}
|
||||
|
||||
fn options(&self) -> Vec<CmdOption> {
|
||||
if self.placing_text {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut options = Vec::new();
|
||||
if self.objects {
|
||||
options.push(CmdOption::new("Objects", "O"));
|
||||
options.push(CmdOption::new("Block", "B"));
|
||||
}
|
||||
if self.text.is_some() {
|
||||
options.push(CmdOption::new("Text", "T"));
|
||||
}
|
||||
options
|
||||
}
|
||||
|
||||
fn wants_text_input(&self) -> bool {
|
||||
!self.placing_text
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||
if self.placing_text {
|
||||
return None;
|
||||
}
|
||||
match text.trim().trim_start_matches('_').to_ascii_uppercase().as_str() {
|
||||
"O" | "OBJECTS" if self.objects => Some(CmdResult::Dispatch("PASTECLIP".into())),
|
||||
"B" | "BLOCK" if self.objects => Some(CmdResult::Dispatch("PASTEBLOCK".into())),
|
||||
"T" | "TEXT" if self.text.is_some() => {
|
||||
self.placing_text = true;
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: DVec3) -> CmdResult {
|
||||
let Some(value) = self.text.clone().filter(|_| self.placing_text) else {
|
||||
return CmdResult::NeedPoint;
|
||||
};
|
||||
let p = self.plane.to_local(pt);
|
||||
let mut mtext = MText::new();
|
||||
mtext.value = value;
|
||||
mtext.insertion_point = Vector3::new(p.x, p.y, p.z);
|
||||
mtext.height = self.height;
|
||||
mtext.style = self.style.clone();
|
||||
mtext.attachment_point = AttachmentPoint::TopLeft;
|
||||
mtext.rectangle_width = 0.0;
|
||||
CmdResult::CommitAndExit(self.plane.place_entity(EntityType::MText(mtext)))
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
if self.placing_text {
|
||||
return CmdResult::NeedPoint;
|
||||
}
|
||||
// Enter takes the first format offered.
|
||||
let first = self.options().first().map(|o| o.keyword.clone());
|
||||
first
|
||||
.and_then(|keyword| self.on_text_input(&keyword))
|
||||
.unwrap_or(CmdResult::Cancel)
|
||||
}
|
||||
}
|
||||
|
||||
inventory::submit!(crate::command::CommandRegistration { names: &["PASTESPEC"] });
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::mtext_value;
|
||||
|
||||
#[test]
|
||||
fn plain_text_becomes_escaped_paragraphs() {
|
||||
assert_eq!(mtext_value("a{b}\\c\r\nline 2\n"), "a\\{b\\}\\\\c\\Pline 2");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user