-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheditor.js
130 lines (110 loc) · 3.04 KB
/
editor.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
119
120
121
122
123
124
125
126
127
128
129
130
'use strict'
const createQuery = require('./')
const GraphiQL = require('graphiql')
const ReactDOM = require('react-dom')
const React = require('react')
const ready = require('domready')
const vm = require('vm')
let query
let currentQuerySource = null
const kDefaultQuery = `
## Renders a scene into a viewport with
## a PerspectiveCamera and a Scene containing
## a Mesh constructed with a BoxGeometry and
## a MeshBasicMaterial
query render($width: Float,
$height: Float,
#$rotateX: Float,
#$rotateY: Float
$tickr: Float) {
## Describes renderer
WebGLRenderer {
## set viewport width/height based on context inputs
setSize(width: $width, height: $height)
## Configures a PerspectiveCamera
PerspectiveCamera(fov: 75, aspect: 1, near: 1, far: 10000) {
setPosition(z: 500)
}
## Describes a Scene
Scene {
## Describes a named Mesh
a: Mesh(name: "box-a") {
setRotation(x: $tickr)
setPosition(x: 200, y: 200)
...BoxWireframe
}
b: Mesh(name: "box-b") {
setPosition(x: -200, y: -200)
setRotation(y: $tickr)
...Box
}
c: Mesh(name: "box-c") {
setRotation(z: $tickr)
setPosition(x: 0)
...Box
}
}
}
}
fragment BoxWireframe on Mesh {
## Contruct geometry and material for mesh
BoxGeometry(width: 100, height: 100, depth: 100)
MeshBasicMaterial(wireframe: true)
}
fragment Box on Mesh {
BoxGeometry(width: 100, height: 100, depth: 100)
MeshBasicMaterial(wireframe: false)
}
`
ready(() => {
const canvas = document.querySelector('#viewport canvas')
query = createQuery({canvas})
requestAnimationFrame(tick)
function tick() {
requestAnimationFrame(tick)
const wrap = document.querySelector('#graphiql .resultWrap')
const style = wrap ? getComputedStyle(wrap) : {
width: window.innerWidth * .49,
height: window.innerHeight - 48
}
const width = parseFloat(style.width)
const height = parseFloat(style.height)
query.context.set('width', width)
query.context.set('height', height)
query(currentQuerySource || kDefaultQuery)
.catch((err) => console.error(err))
}
})
ready(() => {
const domElement = document.getElementById('graphiql')
const reactElement = React.createElement(GraphiQL, {
query: currentQuerySource || kDefaultQuery,
fetcher: fetcher,
variables: '',
onEditQuery: onEditQuery,
onEditVariables: onEditVariables,
})
ReactDOM.render(reactElement, domElement)
function fetcher(params) {
let vars = {}
if (params.variables) {
try { vars = JSON.parse(params.variables) }
catch (e) { }
for (let key in vars) {
query.context.set(key, vars[key])
}
}
return query(params.query)
}
function onEditQuery (q) {
currentQuerySource = q
}
function onEditVariables(value) {
let parsed
try { parsed = JSON.parse(value) }
catch (e) { }
if (parsed && 'object' == typeof parsed) {
query.context.extend(parsed)
}
}
})