Skip to content

Commit e7b318c

Browse files
committed
Convert DefinitionIdentifier to string in JavaScript
1 parent 2acf93b commit e7b318c

8 files changed

Lines changed: 40 additions & 41 deletions

File tree

editor/src/messages/frontend/frontend_message.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::utility_types::{DocumentDetails, MouseCursorIcon, OpenDocument};
22
use crate::messages::app_window::app_window_message_handler::AppWindowPlatform;
33
use crate::messages::input_mapper::utility_types::misc::ActionShortcut;
44
use crate::messages::layout::utility_types::widget_prelude::*;
5-
use crate::messages::portfolio::document::node_graph::document_node_definitions::DefinitionIdentifier;
65
use crate::messages::portfolio::document::node_graph::utility_types::{
76
BoxSelection, ContextMenuInformation, FrontendClickTargets, FrontendGraphInput, FrontendGraphOutput, FrontendNode, FrontendNodeType, NodeGraphErrorDiagnostic, Transform,
87
};
@@ -61,7 +60,7 @@ pub enum FrontendMessage {
6160
// Send prefix: Send global, static data to the frontend that is never updated
6261
SendUIMetadata {
6362
#[serde(rename = "nodeDescriptions")]
64-
node_descriptions: Vec<(DefinitionIdentifier, String)>,
63+
node_descriptions: Vec<(String, String)>,
6564
#[serde(rename = "nodeTypes")]
6665
node_types: Vec<FrontendNodeType>,
6766
},

editor/src/messages/portfolio/document/node_graph/document_node_definitions.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,29 +81,29 @@ impl DefinitionIdentifier {
8181
}),
8282
}
8383
}
84+
85+
pub fn serialized(&self) -> String {
86+
match self {
87+
DefinitionIdentifier::ProtoNode(id) => {
88+
format!("Proto Node:{}", id.as_str())
89+
}
90+
DefinitionIdentifier::Network(data) => {
91+
format!("Network:{}", data)
92+
}
93+
}
94+
}
8495
}
8596

8697
impl From<Value> for DefinitionIdentifier {
8798
fn from(value: Value) -> Self {
88-
match value {
89-
Value::Object(mut map) => {
90-
let ty = map.remove("type").unwrap().as_str().unwrap().to_owned();
99+
let s = value.as_str().expect("DefinitionIdentifier value must be a string");
91100

92-
match ty.as_ref() {
93-
"Network" => {
94-
let data = map.remove("data").unwrap().as_str().unwrap().to_owned();
95-
DefinitionIdentifier::Network(data)
96-
}
97-
"ProtoNode" => {
98-
let value = map.remove("data").unwrap();
99-
let proto: ProtoNodeIdentifier = serde_json::from_value(value).unwrap();
100-
DefinitionIdentifier::ProtoNode(proto)
101-
}
102-
_ => panic!("Unknown `DefinitionIdentifier` type: {:?}", ty),
103-
}
104-
}
101+
let (ty, data) = s.split_once(':').expect("Invalid DefinitionIdentifier key format");
105102

106-
_ => panic!("Expected a JSON object to convert to `DefinitionIdentifier`"),
103+
match ty {
104+
"Proto Node" => DefinitionIdentifier::ProtoNode(ProtoNodeIdentifier::with_owned_string(data.to_string())),
105+
"Network" => DefinitionIdentifier::Network(data.to_string()),
106+
other => panic!("Unknown DefinitionIdentifier type: {other}"),
107107
}
108108
}
109109
}
@@ -2682,7 +2682,7 @@ pub fn collect_node_types() -> Vec<FrontendNodeType> {
26822682
name = identifier.implementation_name_from_identifier()
26832683
}
26842684
FrontendNodeType {
2685-
identifier: identifier.clone(),
2685+
identifier: identifier.serialized(),
26862686
name,
26872687
category: definition.category.to_string(),
26882688
input_types,
@@ -2691,10 +2691,15 @@ pub fn collect_node_types() -> Vec<FrontendNodeType> {
26912691
.collect()
26922692
}
26932693

2694-
pub fn collect_node_descriptions() -> Vec<(DefinitionIdentifier, String)> {
2694+
pub fn collect_node_descriptions() -> Vec<(String, String)> {
26952695
DOCUMENT_NODE_TYPES
26962696
.iter()
2697-
.map(|(identifier, definition)| (identifier.clone(), if definition.description != "TODO" { definition.description.to_string() } else { String::new() }))
2697+
.map(|(identifier, definition)| {
2698+
(
2699+
identifier.serialized(),
2700+
if definition.description != "TODO" { definition.description.to_string() } else { String::new() },
2701+
)
2702+
})
26982703
.collect()
26992704
}
27002705

editor/src/messages/portfolio/document/node_graph/node_graph_message_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2599,7 +2599,7 @@ impl NodeGraphMessageHandler {
25992599
.node_metadata(&node_id, breadcrumb_network_path)
26002600
.is_some_and(|node_metadata| node_metadata.persistent_metadata.is_layer()),
26012601
can_be_layer: network_interface.is_eligible_to_be_layer(&node_id, breadcrumb_network_path),
2602-
reference: network_interface.reference(&node_id, breadcrumb_network_path),
2602+
reference: network_interface.reference(&node_id, breadcrumb_network_path).map(|reference| reference.serialized()),
26032603
display_name: network_interface.display_name(&node_id, breadcrumb_network_path),
26042604
implementation_name: network_interface.implementation_name(&node_id, breadcrumb_network_path),
26052605
primary_input,

editor/src/messages/portfolio/document/node_graph/utility_types.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::document_node_definitions::DefinitionIdentifier;
21
use glam::{DVec2, IVec2};
32
use graph_craft::document::NodeId;
43
use graph_craft::document::value::TaggedValue;
@@ -79,7 +78,7 @@ pub struct FrontendNode {
7978
pub is_layer: bool,
8079
#[serde(rename = "canBeLayer")]
8180
pub can_be_layer: bool,
82-
pub reference: Option<DefinitionIdentifier>,
81+
pub reference: Option<String>,
8382
#[serde(rename = "displayName")]
8483
pub display_name: String,
8584
#[serde(rename = "implementationName")]
@@ -104,7 +103,7 @@ pub struct FrontendNode {
104103

105104
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, specta::Type)]
106105
pub struct FrontendNodeType {
107-
pub identifier: DefinitionIdentifier,
106+
pub identifier: String,
108107
pub name: String,
109108
pub category: String,
110109
#[serde(rename = "inputTypes")]

frontend/src/components/floating-menus/NodeCatalog.svelte

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<script lang="ts">
22
import { createEventDispatcher, getContext, onMount } from "svelte";
33
4-
import type { DefinitionIdentifier, FrontendNodeType } from "@graphite/messages";
4+
import type { FrontendNodeType } from "@graphite/messages";
55
import type { NodeGraphState } from "@graphite/state-providers/node-graph";
66
77
import LayoutCol from "@graphite/components/layout/LayoutCol.svelte";
88
import TextButton from "@graphite/components/widgets/buttons/TextButton.svelte";
99
import TextInput from "@graphite/components/widgets/inputs/TextInput.svelte";
1010
import TextLabel from "@graphite/components/widgets/labels/TextLabel.svelte";
1111
12-
const dispatch = createEventDispatcher<{ selectNodeType: DefinitionIdentifier }>();
12+
const dispatch = createEventDispatcher<{ selectNodeType: string }>();
1313
const nodeGraph = getContext<NodeGraphState>("nodeGraph");
1414
1515
// Content
@@ -125,10 +125,7 @@
125125
{disabled}
126126
label={nodeType.name}
127127
tooltipLabel={nodeType.name}
128-
tooltipDescription={[...$nodeGraph.nodeDescriptions.entries()]
129-
.find(([key]) => key.type === nodeType.identifier?.type && key.data === nodeType.identifier?.data)
130-
?.at(1)
131-
?.toString()}
128+
tooltipDescription={nodeType.identifier ? $nodeGraph.nodeDescriptions.get(nodeType.identifier) : undefined}
132129
action={() => dispatch("selectNodeType", nodeType.identifier)}
133130
/>
134131
{/each}

frontend/src/components/views/Graph.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { fade } from "svelte/transition";
55
66
import type { Editor } from "@graphite/editor";
7-
import type { DefinitionIdentifier, FrontendGraphInput, FrontendGraphOutput, FrontendNode } from "@graphite/messages";
7+
import type { FrontendGraphInput, FrontendGraphOutput, FrontendNode } from "@graphite/messages";
88
import type { NodeGraphState } from "@graphite/state-providers/node-graph";
99
1010
import NodeCatalog from "@graphite/components/floating-menus/NodeCatalog.svelte";
@@ -100,7 +100,7 @@
100100
return sparse;
101101
}
102102
103-
function createNode(identifier: DefinitionIdentifier) {
103+
function createNode(identifier: string) {
104104
if ($nodeGraph.contextMenuInformation === undefined) return;
105105
106106
editor.handle.createNode(identifier, $nodeGraph.contextMenuInformation.contextMenuCoordinates.x, $nodeGraph.contextMenuInformation.contextMenuCoordinates.y);
@@ -481,7 +481,7 @@
481481
{@const layerAreaWidth = $nodeGraph.layerWidths.get(node.id) || 8}
482482
{@const layerChainWidth = $nodeGraph.chainWidths.get(node.id) || 0}
483483
{@const hasLeftInputWire = $nodeGraph.hasLeftInputWire.get(node.id) || false}
484-
{@const description = [...$nodeGraph.nodeDescriptions.entries()].find(([key]) => key.type === node.reference?.type && key.data === node.reference?.data)?.at(1) || undefined}
484+
{@const description = node.reference ? $nodeGraph.nodeDescriptions.get(node.reference) : undefined}
485485
<div
486486
class="layer"
487487
class:selected={$nodeGraph.selected.includes(node.id)}
@@ -634,7 +634,7 @@
634634
.map(([_, node], nodeIndex) => ({ node, nodeIndex })) as { node, nodeIndex } (nodeIndex)}
635635
{@const exposedInputsOutputs = zipWithUndefined(node.exposedInputs, node.exposedOutputs)}
636636
{@const clipPathId = String(Math.random()).substring(2)}
637-
{@const description = [...$nodeGraph.nodeDescriptions.entries()].find(([key]) => key.type === node.reference?.type && key.data === node.reference?.data)?.at(1) || undefined}
637+
{@const description = node.reference ? $nodeGraph.nodeDescriptions.get(node.reference) : undefined}
638638
<div
639639
class="node"
640640
class:selected={$nodeGraph.selected.includes(node.id)}

frontend/src/messages.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class UpdateNodeGraphTransform extends JsMessage {
102102

103103
export class SendUIMetadata extends JsMessage {
104104
@Transform(({ obj }) => new Map(obj.nodeDescriptions))
105-
readonly nodeDescriptions!: Map<DefinitionIdentifier, string>;
105+
readonly nodeDescriptions!: Map<string, string>;
106106

107107
readonly nodeTypes!: FrontendNodeType[];
108108
}
@@ -220,7 +220,7 @@ export class FrontendNode {
220220

221221
readonly canBeLayer!: boolean;
222222

223-
readonly reference!: DefinitionIdentifier | undefined;
223+
readonly reference!: string | undefined;
224224

225225
readonly displayName!: string;
226226

@@ -251,7 +251,7 @@ export class FrontendNode {
251251
}
252252

253253
export class FrontendNodeType {
254-
readonly identifier!: DefinitionIdentifier;
254+
readonly identifier!: string;
255255

256256
readonly name!: string;
257257

frontend/src/state-providers/node-graph.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
type FrontendNode,
1010
type FrontendNodeType,
1111
type WirePath,
12-
type DefinitionIdentifier,
1312
ClearAllNodeGraphWires,
1413
SendUIMetadata,
1514
UpdateBox,
@@ -45,7 +44,7 @@ export function createNodeGraphState(editor: Editor) {
4544
/// The index is the exposed input index. The exports have a first key value of u32::MAX.
4645
wires: new Map<bigint, Map<number, WirePath>>(),
4746
wirePathInProgress: undefined as WirePath | undefined,
48-
nodeDescriptions: new Map<DefinitionIdentifier, string>(),
47+
nodeDescriptions: new Map<string, string>(),
4948
nodeTypes: [] as FrontendNodeType[],
5049
thumbnails: new Map<bigint, string>(),
5150
selected: [] as bigint[],

0 commit comments

Comments
 (0)