Skip to content

Commit 360286a

Browse files
committed
Add 'Map String' and 'Read String' nodes
1 parent edde218 commit 360286a

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

node-graph/nodes/gcore/src/logic.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use convert_case::{Boundary, Converter, pattern};
22
use core_types::Color;
33
use core_types::registry::types::{SignedInteger, TextArea};
44
use core_types::table::Table;
5-
use core_types::{Context, Ctx};
5+
use core_types::{CloneVarArgs, Context, Ctx, ExtractAll, ExtractVarArgs, OwnedContextImpl};
66
use glam::{DAffine2, DVec2};
77
use graphic_types::vector_types::GradientStops;
88
use graphic_types::{Artboard, Graphic, Vector};
@@ -575,3 +575,34 @@ async fn switch<T, C: Send + 'n + Clone>(
575575
) -> T {
576576
if condition { if_true.eval(ctx).await } else { if_false.eval(ctx).await }
577577
}
578+
579+
/// Iterates over a list of strings, evaluating the mapped operation for each one. Use the *Read String* node to access the current string inside the loop.
580+
#[node_macro::node(category("Text"))]
581+
async fn map_string(
582+
ctx: impl Ctx + CloneVarArgs + ExtractAll,
583+
strings: Vec<String>,
584+
#[expose]
585+
#[implementations(Context -> String)]
586+
mapped: impl Node<Context<'static>, Output = String>,
587+
) -> Vec<String> {
588+
let mut result = Vec::new();
589+
590+
for (i, string) in strings.into_iter().enumerate() {
591+
let owned_ctx = OwnedContextImpl::from(ctx.clone());
592+
let owned_ctx = owned_ctx.with_vararg(Box::new(string)).with_index(i);
593+
let mapped_strings = mapped.eval(owned_ctx.into_context()).await;
594+
595+
result.push(mapped_strings);
596+
}
597+
598+
result
599+
}
600+
601+
/// Reads the current string from within a **Map String** node's loop.
602+
#[node_macro::node(category("Context"))]
603+
fn read_string(ctx: impl Ctx + ExtractVarArgs) -> String {
604+
let Ok(var_arg) = ctx.vararg(0) else { return String::new() };
605+
let var_arg = var_arg as &dyn std::any::Any;
606+
607+
var_arg.downcast_ref::<String>().cloned().unwrap_or_default()
608+
}

0 commit comments

Comments
 (0)