diff --git a/crates/doc/src/node.rs b/crates/doc/src/node.rs
index 34c7660b1..7bb419b43 100644
--- a/crates/doc/src/node.rs
+++ b/crates/doc/src/node.rs
@@ -239,7 +239,7 @@ pub struct Annotation {
/// (`figcraft_engine::code_connect`).
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CodeLink {
- /// `react`, `leptos`, `vue`, `swiftui`, `compose`, `arkui` or `flutter`.
+ /// `react`, `leptos`, `vue`, `swiftui`, `compose`, `arkui`, `flutter` or `raw`.
pub language: String,
/// What the snippet imports from (`@acme/ui`, a Swift module, a Kotlin name).
#[serde(default, skip_serializing_if = "String::is_empty")]
diff --git a/crates/engine/src/code_connect_raw_tests.rs b/crates/engine/src/code_connect_raw_tests.rs
new file mode 100644
index 000000000..bf7baaa45
--- /dev/null
+++ b/crates/engine/src/code_connect_raw_tests.rs
@@ -0,0 +1,68 @@
+//! Code connect's raw language (#2424): a template language with no import line, such as Blade,
+//! Django, ERB or Twig, gets its template back as written, values in place and unescaped.
+
+use crate::Session;
+use crate::code_connect_tests::{card, code};
+use crate::components::tests::{ex, idv};
+use serde_json::json;
+
+#[test]
+fn a_raw_template_comes_back_as_written_with_the_values_in_place() {
+ let mut s = Session::new();
+ let c = card(&mut s);
+ let i = idv(&ex(&mut s, "instance.create", json!({"component": c, "x": 300, "y": 0})));
+ ex(&mut s, "instance.setProperties", json!({"ids": [i], "properties": {"Show dot": false, "Title": "Say \"hi\" & bye"}}));
+ let template = "{Show dot ? \"•\" : \"\"}";
+ let v = ex(&mut s, "codeConnect.set", json!({"id": i, "language": "raw", "name": "card", "template": template}));
+ let want = "bye\" dot=\"false\" :size=\"$size\">\n";
+ assert_eq!(v["code"], want, "no import line, nothing escaped");
+ assert_eq!(code(&mut s, i, "raw"), want);
+ assert_eq!(ex(&mut s, "inspect.code", json!({"id": i, "language": "raw"}))["connected"], true);
+ assert!(code(&mut s, i, "react").contains("function"), "the React code is still generated");
+ // A raw template has no import line to take a path.
+ assert!(s.execute("codeConnect.set", &json!({"id": c, "language": "raw", "importPath": "app/Livewire"})).is_err());
+ // It has no component definition either.
+ assert!(s.execute("inspect.componentCode", &json!({"id": i, "language": "raw"})).is_err());
+ // Out to the connect file and into another design.
+ let text = ex(&mut s, "codeConnect.export", json!({}))["text"].as_str().unwrap().to_string();
+ let mut t = Session::new();
+ let c2 = card(&mut t);
+ assert_eq!(ex(&mut t, "codeConnect.import", json!({"json": text}))["linked"], 1);
+ assert!(code(&mut t, c2, "raw").starts_with(""}));
+ let f = idv(&ex(&mut s, "frame.create", json!({"x": 0, "y": 100, "width": 120, "height": 40, "name": "Button"})));
+ let nested = idv(&ex(&mut s, "instance.create", json!({"component": heart, "x": 8, "y": 108, "parent": f})));
+ let button = idv(&ex(&mut s, "component.create", json!({"ids": [f]})));
+ ex(&mut s, "component.addProperty", json!({"id": button, "name": "Icon", "type": "instanceSwap", "default": heart, "layer": nested}));
+ let i = idv(&ex(&mut s, "instance.create", json!({"component": button, "x": 0, "y": 300})));
+ ex(&mut s, "instance.setProperties", json!({"ids": [i], "properties": {"Icon": star}}));
+ ex(&mut s, "codeConnect.set", json!({"id": button, "language": "raw", "name": "button", "template": ""}));
+ assert_eq!(code(&mut s, i, "raw"), "\n");
+}
diff --git a/crates/engine/src/code_connect_tests.rs b/crates/engine/src/code_connect_tests.rs
index f28aa882c..d9c7c37cc 100644
--- a/crates/engine/src/code_connect_tests.rs
+++ b/crates/engine/src/code_connect_tests.rs
@@ -7,12 +7,12 @@ use crate::{EngineError, Session};
use figcraft_doc::*;
use serde_json::{Value, json};
-fn code(s: &mut Session, id: NodeId, lang: &str) -> String {
+pub(crate) fn code(s: &mut Session, id: NodeId, lang: &str) -> String {
ex(s, "inspect.code", json!({"id": id, "language": lang}))["code"].as_str().unwrap_or_default().to_string()
}
/// A "Card" component with a "Title" text property and a "Show dot" boolean property.
-fn card(s: &mut Session) -> NodeId {
+pub(crate) fn card(s: &mut Session) -> NodeId {
let f = idv(&ex(s, "frame.create", json!({"x": 0, "y": 0, "width": 200, "height": 100, "name": "Card"})));
let title = idv(&ex(s, "text.create", json!({"text": "Hello", "x": 10, "y": 10, "parent": f, "props": {"name": "Title"}})));
let dot = idv(&ex(s, "shape.ellipse", json!({"x": 170, "y": 10, "width": 20, "height": 20, "parent": f, "props": {"name": "Dot"}})));
diff --git a/crates/engine/src/dev/code_connect.rs b/crates/engine/src/dev/code_connect.rs
index 17866b7c8..df97f4f0a 100644
--- a/crates/engine/src/dev/code_connect.rs
+++ b/crates/engine/src/dev/code_connect.rs
@@ -43,7 +43,17 @@ const MAX_WARNINGS: usize = 50;
/// The languages a component can be connected in.
pub fn language(s: &str) -> Option {
CodeLang::from_id(s).filter(|l| {
- matches!(l, CodeLang::React | CodeLang::Leptos | CodeLang::Vue | CodeLang::SwiftUi | CodeLang::Compose | CodeLang::ArkUi | CodeLang::Flutter)
+ matches!(
+ l,
+ CodeLang::React
+ | CodeLang::Leptos
+ | CodeLang::Vue
+ | CodeLang::SwiftUi
+ | CodeLang::Compose
+ | CodeLang::ArkUi
+ | CodeLang::Flutter
+ | CodeLang::Raw
+ )
})
}
@@ -281,7 +291,14 @@ fn expr(doc: &Document, c: &Component, p: &Param, v: &Value, lang: CodeLang) ->
(Some(l), CodeLang::SwiftUi) => format!("{}()", l.name),
(Some(l), CodeLang::ArkUi) => format!("() => {{ {}() }}", l.name),
(Some(l), CodeLang::Flutter) => format!("{}()", l.name),
+ (Some(l), CodeLang::Raw) => l.name.clone(),
(Some(l), _) => format!("{{ {}() }}", l.name),
+ // A raw template takes the value as text: no quotes or escapes.
+ (None, CodeLang::Raw) => match v {
+ Value::String(s) => s.clone(),
+ Value::Null => String::new(),
+ v => v.to_string(),
+ },
(None, _) => value_expr(doc, c, p, v, lang),
}
}
@@ -478,6 +495,7 @@ fn default_template(c: &Component, lang: CodeLang, name: &str) -> String {
CodeLang::Vue => format!("<{name}{} />", params.iter().map(|p| format!(" :{p}={{{p}}}")).collect::()),
CodeLang::SwiftUi | CodeLang::Flutter => format!("{name}({})", params.iter().map(|p| format!("{p}: {{{p}}}")).collect::>().join(", ")),
CodeLang::ArkUi => format!("{name}({{ {} }})", params.iter().map(|p| format!("{p}: {{{p}}}")).collect::>().join(", ")),
+ CodeLang::Raw => format!("<{name}{} />", params.iter().map(|p| format!(" {p}=\"{{{p}}}\"")).collect::()),
_ => format!("{name}({})", params.iter().map(|p| format!("{p} = {{{p}}}")).collect::>().join(", ")),
}
}
@@ -508,6 +526,7 @@ pub fn check(c: &Component, link: &CodeLink, lang: CodeLang) -> std::result::Res
CodeLang::Leptos => path.chars().all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == ':'),
CodeLang::Vue => path.chars().all(|ch| ch.is_ascii_alphanumeric() || "_./@-".contains(ch)),
CodeLang::Flutter => path.chars().all(|ch| ch.is_ascii_alphanumeric() || "_./:-".contains(ch)),
+ CodeLang::Raw => path.is_empty(),
_ => path.chars().all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '.'),
};
if path.chars().count() > MAX_IMPORT || !path_ok {
@@ -516,6 +535,7 @@ pub fn check(c: &Component, link: &CodeLink, lang: CodeLang) -> std::result::Res
CodeLang::ArkUi => format!("importPath: a module path without quotes, up to {MAX_IMPORT} characters"),
CodeLang::Leptos => format!("importPath: a Rust module path (letters, digits, _ and ::), up to {MAX_IMPORT} characters"),
CodeLang::Vue => format!("importPath: a module path (letters, digits, _ . / @ and -), up to {MAX_IMPORT} characters"),
+ CodeLang::Raw => "importPath: a raw template has no import line; leave it empty".into(),
CodeLang::Flutter => {
format!("importPath: a Dart import (package:acme/card.dart; letters, digits, _ . / : and -), up to {MAX_IMPORT} characters")
}
@@ -568,7 +588,7 @@ fn text(cmd: &str, p: &Value, key: &str) -> Result