From 9d73651b2a4fdf15c940cb7b1ba790fda178b6a0 Mon Sep 17 00:00:00 2001
From: Dennis Collaris <d.collaris@me.com>
Date: Wed, 4 Dec 2024 13:06:46 +0100
Subject: [PATCH] feat: add import/export capabilities to variable nodes

---
 .../components/textEditor/VariableNode.tsx    | 26 ++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/libs/shared/lib/components/textEditor/VariableNode.tsx b/libs/shared/lib/components/textEditor/VariableNode.tsx
index 300242055..a2341e498 100644
--- a/libs/shared/lib/components/textEditor/VariableNode.tsx
+++ b/libs/shared/lib/components/textEditor/VariableNode.tsx
@@ -1,4 +1,4 @@
-import type { NodeKey, LexicalEditor, DOMExportOutput } from 'lexical';
+import type { NodeKey, LexicalEditor, DOMExportOutput, SerializedLexicalNode, Spread } from 'lexical';
 import { DecoratorNode, EditorConfig } from 'lexical';
 
 export enum VariableType {
@@ -6,6 +6,14 @@ export enum VariableType {
   visualization = 'visualization',
 }
 
+export type SerializedVariableNode = Spread<
+  {
+    name: string,
+    variableType: VariableType
+  },
+  SerializedLexicalNode
+>;
+
 export class VariableNode extends DecoratorNode<JSX.Element> {
   __name: string;
   __variableType: VariableType;
@@ -39,6 +47,22 @@ export class VariableNode extends DecoratorNode<JSX.Element> {
     return `{{ ${self.__variableType}:${self.__name} }}`;
   }
 
+  // Import and export
+
+  exportJSON(): SerializedVariableNode {
+    return {
+      type: this.getType(),
+      variableType: this.__variableType,
+      name: this.__name,
+      version: 1,
+    };
+  }
+
+  static importJSON(jsonNode: SerializedVariableNode): VariableNode {
+    const node = new VariableNode(jsonNode.name, jsonNode.variableType);
+    return node;
+  }
+
   // View
 
   createDOM(config: EditorConfig): HTMLElement {
-- 
GitLab