forked from Pagedraw/pagedraw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library-index.coffee
53 lines (43 loc) · 1.71 KB
/
library-index.coffee
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
# polyfill in case we're on old node js, which naturally doesn't have exotic
# built in string methods like .endsWith()
require('string.prototype.endswith')
# initialize the compiler
require('./src/load_compiler')
_l = require 'lodash'
React = require 'react'
{Doc} = require './src/doc'
core = require './src/core'
{foreachPdom} = require './src/pdom'
Dynamic = require './src/dynamic'
evalPdom = require './src/eval-pdom'
{pdomToReact} = require './src/editor/pdom-to-react'
util = require './src/util'
exports.get_components = (pd_json_obj) ->
# parse and deserialize the doc
doc = Doc.deserialize(pd_json_obj)
doc.enterReadonlyMode()
getCompiledComponentByUniqueKey = util.memoized_on _l.identity, (uniqueKey) ->
componentBlockTree = doc.getBlockTreeByUniqueKey(uniqueKey)
return undefined if not componentBlockTree?
pdom = core.compileComponent(componentBlockTree, {
templateLang: 'JSX'
for_editor: false
for_component_instance_editor: false
getCompiledComponentByUniqueKey: getCompiledComponentByUniqueKey
})
core.foreachPdom pdom, (pd) ->
pd.boxSizing = 'border-box'
if pd.event_handlers?
pd[event + "Attr"] = new Dynamic(code, undefined) for {event, code} in pd.event_handlers
delete pd.event_handlers
return pdom
# compile the doc
return _l.mapValues _l.keyBy(doc.getComponents(), 'name'), (component) ->
return (props) ->
pdom = evalPdom(
{tag: component, props},
getCompiledComponentByUniqueKey,
'JSX',
window.innerWidth
)
return pdomToReact(pdom)