From 1ed6e43da4b38898031b7c63eb0fdd1909c685e5 Mon Sep 17 00:00:00 2001
From: Max <m.giller.dev@gmail.com>
Date: Fri, 15 Oct 2021 14:28:32 +0200
Subject: [PATCH] Made new storage interface more accessible for graph and
 editor and simplified imports

---
 config.js           |  3 ++-
 display/graph.js    |  2 +-
 editor/js/editor.js |  3 ++-
 knowledge-space.php | 40 +++++++++++++++++++++++-----------------
 4 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/config.js b/config.js
index e1c17d8..2e077a9 100644
--- a/config.js
+++ b/config.js
@@ -9,4 +9,5 @@ export const COLOR_PALETTE = [
 ];
 
 // Just renaming a variable which is given by the PHP script. This avoids errors in all other files.
-export const PLUGIN_PATH = plugin_path;
+export const PLUGIN_PATH = plugin.path;
+export const SPACE = space.id;
diff --git a/display/graph.js b/display/graph.js
index 32ba3da..351feff 100644
--- a/display/graph.js
+++ b/display/graph.js
@@ -520,7 +520,7 @@ let infoOverlay = null;
 
 // Only execute, if corresponding dom is present
 if (document.getElementById("3d-graph") !== null) {
-    G = new Graph(space_id); // space_id defined globaly through knowledge-space.php
+    G = new Graph(Config.SPACE); // space_id defined globaly through knowledge-space.php
     filterOverlay = new FilterOverlay(G, "node");
     infoOverlay = new NodeInfoOverlay(G);
     G.infoOverlay = infoOverlay;
diff --git a/editor/js/editor.js b/editor/js/editor.js
index 8473bf1..a361e4e 100644
--- a/editor/js/editor.js
+++ b/editor/js/editor.js
@@ -3,6 +3,7 @@ import * as Graph from "./graph";
 import { loadGraphJson } from "../../datasets/datasets";
 import ForceGraph from "force-graph";
 import * as Interactions from "./interactions";
+import { SPACE } from "../../config";
 
 
 export var state;
@@ -20,7 +21,7 @@ window.onload = function () {
 
     Interactions.initInteractions();
 
-    loadGraphJson(space_id) // space_id defined globaly through knowledge-space.php
+    loadGraphJson(SPACE) // space_id defined globaly through knowledge-space.php
         .then((graphConfig) => {
             state = new State();
             graph = new Graph.Graph(graphConfig);
diff --git a/knowledge-space.php b/knowledge-space.php
index 8b85da0..53dc519 100644
--- a/knowledge-space.php
+++ b/knowledge-space.php
@@ -14,17 +14,21 @@ function ks_add_graph($atts = []): string
     $space_id = get_space_id_from_atts($atts);
     $div = '<div id="3d-graph"></div>'; // The id "3d-graph" indicates, that the javascript associated with this should automatically be executed
     $plugin_dir = plugin_dir_url(__FILE__);
-    //$dataset = $plugin_dir.'datasets/miserables.json';
-    $variables = "<script> 
-                  var plugin_path = '$plugin_dir';
-                  var space_id = '$space_id';
-                  </script>";
-
-    $script_path = $plugin_dir . 'build' . DIRECTORY_SEPARATOR . $GLOBALS['build'] . DIRECTORY_SEPARATOR . 'graph.js';
-    $script = "<script src='$script_path'></script>";
+
+    $script_path = 'build' . DIRECTORY_SEPARATOR . $GLOBALS['build'] . DIRECTORY_SEPARATOR . 'graph.js';
+    // $script = "<script src='$script_path'></script>";
     //wp_enqueue_script('kg-script', $script_path);
+    wp_enqueue_script("ks-display-js", plugins_url($script_path, __FILE__), array('jquery'), false);
+    wp_localize_script(
+        'ks-display-js',
+        'ajax_object',
+        array('ajax_url' => admin_url('admin-ajax.php'))
+    );
+    wp_localize_script("ks-display-js", "space", array('id' => $space_id));
+    wp_localize_script("ks-display-js", "plugin", array('path' => $plugin_dir));
 
-    return $div . $variables . $script;
+    return $div . $variables;
+    // return $div . $variables . $script;
 }
 
 function ks_add_editor($atts = [])
@@ -33,10 +37,8 @@ function ks_add_editor($atts = [])
 
     $space_id = get_space_id_from_atts($atts);
     $plugin_dir = plugin_dir_url(__FILE__);
-    echo "<script> 
-        var plugin_path = '$plugin_dir';
-        var space_id = '$space_id';
-        </script>";
+    wp_localize_script("ks-editor-js", "space", array('id' => $space_id));
+    wp_localize_script("ks-editor-js", "plugin", array('path' => $plugin_dir));
 
     require_once(__DIR__ . '/editor/editor.php');
 }
@@ -45,12 +47,15 @@ function ks_add_editor_dependencies()
 {
     $script_path = 'build' . DIRECTORY_SEPARATOR . $GLOBALS['build'] . DIRECTORY_SEPARATOR . 'graph.js';
 
-//    wp_enqueue_script('jquery');
+    //    wp_enqueue_script('jquery');
     wp_enqueue_script("ks-editor-js", plugins_url($script_path, __FILE__), array('jquery'), false);
     //wp_register_script("ks-editor-js", plugins_url($script_path, __FILE__), array('jquery'), false);
 
-    wp_localize_script( 'ks-editor-js', 'ajax_object',
-        array( 'ajax_url' => admin_url( 'admin-ajax.php' )));
+    wp_localize_script(
+        'ks-editor-js',
+        'ajax_object',
+        array('ajax_url' => admin_url('admin-ajax.php'))
+    );
     //wp_enqueue_script("ks-editor-js");
 
     $style_file_version = date("ymd-Gis", filemtime(plugin_dir_path(__FILE__) . "editor/css/editor.css"));
@@ -76,7 +81,8 @@ function escape_space_id($id)
     );
 }
 
-function get_space_id_from_atts($atts) {
+function get_space_id_from_atts($atts)
+{
     if ($atts != "" && array_key_exists("space", $atts)) {
         return escape_space_id($atts["space"]);
     } else {
-- 
GitLab