|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace neon\cms\components; |
|
5
|
|
|
|
|
6
|
|
|
use DOMDocument; |
|
7
|
|
|
use SimpleXMLElement; |
|
8
|
|
|
use neon\cms\services\cmsEditor\Parser; |
|
9
|
|
|
use neon\core\helpers\Arr; |
|
10
|
|
|
use neon\core\web\View; |
|
11
|
|
|
|
|
12
|
|
|
class Editor |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param array $vdom |
|
17
|
|
|
* @param SimpleXMLElement $node |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function node(&$vdom, $node) |
|
20
|
|
|
{ |
|
21
|
|
|
$i = uuid64(); |
|
22
|
|
|
dp($node); |
|
23
|
|
|
if (!$node instanceof SimpleXMLElement) { |
|
|
|
|
|
|
24
|
|
|
$vdom[$i] = [$i, '#text', $node->nodeValue]; |
|
25
|
|
|
dd('HERE'); |
|
26
|
|
|
} else { |
|
27
|
|
|
$node->addAttribute('data-ni-id', $i); |
|
28
|
|
|
$attributes = []; |
|
29
|
|
|
foreach ($node->attributes() as $attr => $value) { |
|
30
|
|
|
$attributes[$attr] = (string) $value; |
|
31
|
|
|
} |
|
32
|
|
|
$vdom[$i] = [$i, $node->getName(), $attributes, $node->nodeValue]; |
|
33
|
|
|
} |
|
34
|
|
|
foreach ($node as $n) { |
|
35
|
|
|
$vdom[$i]['children'][] = Editor::node($vdom, $n); |
|
36
|
|
|
} |
|
37
|
|
|
return $i; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
public static function render($vdom) |
|
42
|
|
|
{ |
|
43
|
|
|
foreach($vdom as $element) { |
|
44
|
|
|
$element[1]($element[2], $element['children']); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Create a block editor |
|
50
|
|
|
* @param $params |
|
51
|
|
|
* @param $content |
|
52
|
|
|
* @param \Smarty_Internal_Template $template |
|
53
|
|
|
* @param $repeat |
|
54
|
|
|
* @return mixed|string |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function editor($params, $content, \Smarty_Internal_Template $template, &$repeat) |
|
57
|
|
|
{ |
|
58
|
|
|
if ($repeat) return; |
|
59
|
|
|
$id = Arr::get($params, 'id', neon()->cms->getPage()->getId()); |
|
60
|
|
|
$dbContent = neon()->getCms()->getICmsStaticData()->getStaticContent($id, null); |
|
61
|
|
|
if (!$dbContent) { |
|
62
|
|
|
$parse = new Parser(); |
|
63
|
|
|
|
|
64
|
|
|
// build our vdom |
|
65
|
|
|
$x = simplexml_load_string("<div data-ni-editor=\"$id\">$content</div>"); |
|
66
|
|
|
// $domArray = json_decode(json_encode($x), TRUE); |
|
67
|
|
|
// dd($x); |
|
68
|
|
|
// dd($domArray); |
|
69
|
|
|
// Editor::vdom($domArray); |
|
70
|
|
|
|
|
71
|
|
|
$ele=dom_import_simplexml($x); |
|
72
|
|
|
$dom = new DOMDocument('1.0', 'utf-8'); |
|
73
|
|
|
$element=$dom->importNode($ele,true); |
|
74
|
|
|
$element->normalize(); |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$vdom = []; |
|
78
|
|
|
foreach($x as $n) { |
|
79
|
|
|
Editor::node($vdom, $n); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
//Editor::render($vdom); |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$parsedComps = $parse->parse("$content"); |
|
86
|
|
|
$editorProps = [ |
|
87
|
|
|
'components' => $parsedComps, |
|
88
|
|
|
'contentKey' => $id |
|
89
|
|
|
]; |
|
90
|
|
|
// dd($parsedComps); |
|
91
|
|
|
$content = '<!-- ni:editor '.json_encode($editorProps).' --><!-- /ni:editor -->'; |
|
92
|
|
|
} |
|
93
|
|
|
$content = $dbContent ? $dbContent : $content; |
|
94
|
|
|
|
|
95
|
|
|
$props = '{"components":null, "contentKey": "'.$id.'"}'; |
|
96
|
|
|
if ($content !== null) { |
|
97
|
|
|
// read the json data serialised inside the <!-- ni:editor {json data} --> tag |
|
98
|
|
|
$pattern = '/<!--\s+(?P<closer>\/)?ni:editor\s+(?P<attrs>{(?:(?:[^}]+|}+(?=})|(?!}\s+\/?-->).)*+)?}\s+)?(?P<void>\/)?-->/s'; |
|
99
|
|
|
preg_match($pattern, $content, $matches); |
|
100
|
|
|
if ($matches) { |
|
101
|
|
|
$props = $matches['attrs']; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// layout must have an id |
|
106
|
|
|
$layoutId = Arr::get($params, 'id', $id); |
|
|
|
|
|
|
107
|
|
|
$editing = neon()->cms->getPage()->isInEditMode(); |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
// lookup dropzone components data from layout by id |
|
111
|
|
|
// if no layout exists for the id then use parse $content for components |
|
112
|
|
|
// if in edit mode then output the js editor: |
|
113
|
|
|
if ($editing) { |
|
114
|
|
|
$insertHtml = "<div id='$id'></div>"; |
|
115
|
|
|
// register js |
|
116
|
|
|
neon()->view->registerJs(';(function() { |
|
117
|
|
|
var editorC = Vue.extend(Vue.component(\'neon-editor\')); |
|
118
|
|
|
var editor = new editorC({propsData:'.$props.'}); |
|
119
|
|
|
editor.$mount("#'.$id.'") |
|
120
|
|
|
})();', View::POS_READY, $id); |
|
121
|
|
|
return $insertHtml; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
|
125
|
|
|
// Render components from definition: |
|
126
|
|
|
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
|
127
|
|
|
$props = json_decode($props, true); |
|
128
|
|
|
$cmps = $props['components']; |
|
129
|
|
|
$root = $cmps['root']; |
|
130
|
|
|
$node = static::createFromTree($cmps, $root); |
|
131
|
|
|
$ret = static::renderAsSmarty($node); |
|
|
|
|
|
|
132
|
|
|
// dd($ret); |
|
133
|
|
|
// dp($root, $cmps); |
|
134
|
|
|
// $ret = ''; |
|
135
|
|
|
// foreach($root['children'] as $childId) { |
|
136
|
|
|
// $formatParams = ''; foreach($cmps[$childId]['props'] as $key => $value) { |
|
137
|
|
|
// if (is_array($value)) { |
|
138
|
|
|
// $value = var_export($value); |
|
139
|
|
|
// } |
|
140
|
|
|
// $formatParams .= $key.'="'.$value.'" '; |
|
141
|
|
|
// } |
|
142
|
|
|
// dd($cmps[$childId]['children']); |
|
143
|
|
|
// |
|
144
|
|
|
// {cmp cmp="NeonColumns" children=[[cmp=>'NeonCol'], [cmp=>'NeonCol', chukdr] ]} |
|
145
|
|
|
// |
|
146
|
|
|
// |
|
147
|
|
|
// // if its smarty |
|
148
|
|
|
// $ret .= '{'.$cmps[$childId]['cmp'].' '.$formatParams."}\n"; |
|
149
|
|
|
// } |
|
150
|
|
|
// dd($ret); |
|
151
|
|
|
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
|
152
|
|
|
|
|
153
|
|
|
// dd($ret); |
|
154
|
|
|
// {div} |
|
155
|
|
|
// {cmp } |
|
156
|
|
|
// {cmpList } |
|
157
|
|
|
// {cmp NeonColumns} |
|
158
|
|
|
// {cmp NeonColumn} |
|
159
|
|
|
|
|
160
|
|
|
// {col content=""} |
|
161
|
|
|
// {cmp children="['DVEWvdfveqb3weef', 'wefqwefffdRwefj2']"} |
|
162
|
|
|
// {/col} |
|
163
|
|
|
|
|
164
|
|
|
// {grid columns} |
|
165
|
|
|
// {col}{/col} |
|
166
|
|
|
// {col}{/col} |
|
167
|
|
|
// {col}{/col} |
|
168
|
|
|
// {/grid} |
|
169
|
|
|
|
|
170
|
|
|
//dd($ret); |
|
171
|
|
|
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
|
172
|
|
|
// return static::renderString($ret); |
|
173
|
|
|
// just display the html |
|
174
|
|
|
return $content; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Gets a node in format |
|
179
|
|
|
* @param $node [ |
|
|
|
|
|
|
180
|
|
|
* [cmp] => NeonColumns |
|
181
|
|
|
* [uuid] => ZhKVv1BoIL4ctqibMrIgh9 |
|
182
|
|
|
* [children] => Array |
|
183
|
|
|
* [props] => Array |
|
184
|
|
|
*/ |
|
185
|
|
|
public static function renderAsSmarty($node, &$depth=0) |
|
186
|
|
|
{ |
|
187
|
|
|
$depth++; |
|
188
|
|
|
$cmp = $node['cmp']; |
|
189
|
|
|
$uuid = $node['uuid']; |
|
190
|
|
|
$children = $node['children']; |
|
191
|
|
|
$props = $node['props']; |
|
192
|
|
|
$out = '{'."$cmp " . static::formatProps($props) . " uuid=\"$uuid\" }\n"; |
|
193
|
|
|
foreach($children as $child) { |
|
194
|
|
|
$out .= str_repeat(' ', $depth) . static::renderAsSmarty($child, $depth); |
|
195
|
|
|
} |
|
196
|
|
|
$out .= str_repeat(' ', $depth-1) . '{/'.$cmp."}\n"; |
|
197
|
|
|
$depth--; |
|
198
|
|
|
return $out; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public static function formatProps($props) |
|
202
|
|
|
{ |
|
203
|
|
|
unset($props['children']); |
|
204
|
|
|
$formatParams = ''; |
|
205
|
|
|
foreach($props as $key => $value) { |
|
206
|
|
|
if (is_array($value)) $value = var_export($value, true); |
|
207
|
|
|
$formatParams .= $key.'="'. $value .'" '; |
|
208
|
|
|
} |
|
209
|
|
|
return $formatParams; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Recursively walk the tree to nest data |
|
215
|
|
|
* @param $cmpIndex |
|
216
|
|
|
* @param $root |
|
217
|
|
|
* @return mixed |
|
218
|
|
|
*/ |
|
219
|
|
|
public static function createFromTree($cmpIndex, $root) { |
|
220
|
|
|
$out = $root; |
|
221
|
|
|
foreach($root['children'] as $key => $childId) { |
|
222
|
|
|
if (!empty($out['children'])) { |
|
223
|
|
|
$out['children'][$key] = static::createFromTree($cmpIndex, $cmpIndex[$childId]); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
return $out; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public function renderCmp($params) |
|
|
|
|
|
|
230
|
|
|
{ |
|
231
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|