This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
renderer.js
118 lines (108 loc) · 3.56 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React from 'react';
import ReactDOM from 'react-dom';
import GeoJSONComponent from 'jupyterlab_geojson_react';
import './index.css';
const MIME_TYPE = 'application/geo+json';
const CLASS_NAME = 'output_GeoJSON rendered_html';
const DEFAULT_WIDTH = 840;
const DEFAULT_HEIGHT = 360;
/**
* Render data to the DOM node
*/
function render(props, node) {
return ReactDOM.render(<GeoJSONComponent {...props} />, node);
}
/**
* Handle when an output is cleared or removed
*/
function handleClearOutput(event, { cell: { output_area } }) {
/* Get rendered DOM node */
const toinsert = output_area.element.find(`.${CLASS_NAME.split(' ')[0]}`);
/* e.g. Dispose of resources used by renderer library */
if (toinsert[0]) ReactDOM.unmountComponentAtNode(toinsert[0]);
}
/**
* Handle when a new output is added
*/
function handleAddOutput(event, { output, output_area }) {
/* Get rendered DOM node */
const toinsert = output_area.element.find(`.${CLASS_NAME.split(' ')[0]}`);
/** Hack: Leaflet maps don't display all tiles unless the window is
* resized or `map.invalidateSize()` is called.
* https://github.com/Leaflet/Leaflet/issues/694
*/
if (toinsert[0]) output_area.ref.map.invalidateSize();
}
/**
* Register the mime type and append_mime function with the notebook's
* output area
*/
export function register_renderer(notebook, events, OutputArea) {
/* A function to render output of 'application/geo+json' mime type */
const append_mime = function(data, metadata, element) {
/* Create a DOM node to render to */
const toinsert = this.create_output_subarea(
metadata,
CLASS_NAME,
MIME_TYPE
);
this.keyboard_manager.register_events(toinsert);
/* Render data to DOM node */
const props = {
data,
metadata: metadata[MIME_TYPE],
width: element.width(),
height: DEFAULT_HEIGHT
};
this.ref = render(props, toinsert[0]);
element.append(toinsert);
const output_area = this;
this.element.on('changed', () => {
if (output_area.outputs.length > 0) ReactDOM.unmountComponentAtNode(toinsert[0]);
});
return toinsert;
};
/* Handle when an output is cleared or removed */
events.on('clear_output.CodeCell', handleClearOutput);
events.on('delete.Cell', handleClearOutput);
/* Handle when a new output is added */
events.on('output_added.OutputArea', handleAddOutput);
/**
* Calculate the index of this renderer in `output_area.display_order`
* e.g. Insert this renderer after any renderers with mime type that matches
* "+json"
*/
// const mime_types = output_area.mime_types();
// const json_types = mime_types.filter(mimetype => mimetype.includes('+json'));
// const index = mime_types.lastIndexOf(json_types.pop() + 1);
/* ...or just insert it at the top */
const index = 0;
/**
* Register the mime type and append_mime function with output_area
*/
OutputArea.prototype.register_mime_type(MIME_TYPE, append_mime, {
/* Is output safe? */
safe: true,
/* Index of renderer in `output_area.display_order` */
index: index
});
}
/**
* Re-render cells with output data of 'application/geo+json' mime type
* on load notebook
*/
export function render_cells(notebook) {
/* Get all cells in notebook */
notebook.get_cells().forEach(cell => {
/* If a cell has output data of 'application/geo+json' mime type */
if (
cell.output_area &&
cell.output_area.outputs.find(
output => output.data && output.data[MIME_TYPE]
)
) {
/* Re-render the cell */
notebook.render_cell_output(cell);
}
});
}