geomap.tsx 3.63 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import React from "react";
import ReactDOM from "react-dom";

import EditorJsContainer from "react-editor-js";

import EditorJS from "@editorjs/editorjs";
import Embed from "@editorjs/embed";
import Table from "@editorjs/table";
import List from "@editorjs/list";
import Warning from "@editorjs/warning";
import Code from "@editorjs/code";
import LinkTool from "@editorjs/link";
import Image from "@editorjs/image";
import Raw from "@editorjs/raw";
import Header from "@editorjs/header";
import Quote from "@editorjs/quote";
import MarkerEdit from "@editorjs/marker";
import CheckList from "@editorjs/checklist";
import Delimiter from "@editorjs/delimiter";
import InlineCode from "@editorjs/inline-code";
import SimpleImage from "@editorjs/simple-image";

import { client } from "@logilab/cwclientlibjs";

25
26
27
28
29
import {
  ComposableMap,
  Geographies,
  Geography,
  Marker,
30
  Point,
31
32
} from "react-simple-maps";

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
export const EDITOR_JS_TOOLS = {
  embed: Embed,
  table: Table,
  marker: MarkerEdit,
  list: List,
  warning: Warning,
  code: Code,
  linkTool: LinkTool,
  image: Image,
  raw: Raw,
  header: Header,
  quote: Quote,
  checklist: CheckList,
  delimiter: Delimiter,
  inlineCode: InlineCode,
  simpleImage: SimpleImage,
};

const geoUrl =
  "https://raw.githubusercontent.com/zcreativelabs/react-simple-maps/master/topojson-maps/world-110m.json";
53
54

declare const data: {
55
56
57
58
59
60
  name: string;
  latitude: number;
  longitude: number;
  eid: number;
  description: any;
};
61
62
63
64
65
66

const MapChart = () => {
  return (
    <ComposableMap>
      <Geographies geography={geoUrl}>
        {({ geographies }) =>
67
68
69
70
71
72
73
74
          geographies.map((geo) => (
            <Geography
              key={geo.rsmKey}
              geography={geo}
              fill="#EAEAEC"
              stroke="#D6D6DA"
            />
          ))
75
76
        }
      </Geographies>
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
      <Marker coordinates={[data.longitude, data.latitude] as Point}>
        <g
          fill="none"
          stroke="#FF5533"
          strokeWidth="2"
          strokeLinecap="round"
          strokeLinejoin="round"
          transform="translate(-12, -24)"
        >
          <circle cx="12" cy="10" r="3" />
          <path d="M12 21.7C17.3 17 20 13 20 10a8 8 0 1 0-16 0c0 3 2.7 6.9 8 11.7z" />
        </g>
        <text
          textAnchor="middle"
          y={10}
          style={{ fontFamily: "system-ui", fill: "#5D5A6D" }}
        >
          {data.name}
        </text>
      </Marker>
97
98
99
100
101
    </ComposableMap>
  );
};

function App() {
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  // XX to change with some data attribut
  const rqlClient = new client.CwRqlClient(
    new client.CwSimpleHttpClient("http://localhost:8080", true)
  );
  const instanceRef = React.useRef<EditorJS | null>(null);

  const [editorData, setEditorData] = React.useState<any | null>(null);
  async function handleSave() {
    if (instanceRef.current !== null) {
      const savedData = await instanceRef.current.save();
      rqlClient.queryRows(
        `Set M description "${JSON.stringify(savedData).replaceAll(
          '"',
          '\\"'
        )}" Where M is Museum, M eid ${data.eid}`
      );
    }
  }
  React.useEffect(() => {
    rqlClient
      .queryRows(`Any D Where M is Museum, M eid ${data.eid}, M description D`)
      .then((response) => {
        setEditorData(response[0][0]);
      });
  }, []);

  return (
    <>
      <EditorJsContainer
        tools={EDITOR_JS_TOOLS}
        data={JSON.parse(data.description)}
        instanceRef={(instance) => (instanceRef.current = instance)}
        readOnly={false}
      />
      <button onClick={handleSave}>SAVE THIS ! </button>
      <MapChart />
    </>
  );
140
141
142
}

const root = document.getElementById("awesome-map");
143
ReactDOM.render(<App />, root);