-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProcessTextareaPreview.module
executable file
·291 lines (212 loc) · 9.27 KB
/
ProcessTextareaPreview.module
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
require_once 'includes/autoload.php';
/**
* Textarea Preview Module
*
* @author Christian Raunitschka (owzim)
* @copyright Christian Raunitschka
* <http://ch.rauni.me>
*
* ProcessWire 2.x
* Copyright (C) 2014 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
class ProcessTextareaPreview extends TextareaPreviewBaseProcess implements Module, ConfigurableModule {
const TITLE = 'Textarea Preview';
const PAGE_NAME = 'textarea-preview';
const COOKIE_NAME_TEXT = "%s_%s_%s_text";
const COOKIE_NAME_CHANGED = "%s_%s_%s_changed";
const AJAX_FETCH_SEGMENT = "ajaxfetch";
const IFRAME_URL_SEGMENT = "preview";
protected static $defaultConfig = array(
'trackChangesInterval' => array(
'label' => 'Track changes interval',
'value' => 1000,
'inputfieldType' => 'InputfieldInteger',
'attributes' => array(
'icon' => 'refresh',
'columnWidth' => 25,
'type' => 'number',
'min' => 333,
'size' => 5,
'description' => 'The interval in milliseconds changes within the text area are tracked and sent to the server to be run through the text formatters - default is 1000',
)
),
'cssTheme' => array(
'label' => 'CSS Theme',
'value' => 'theme-github',
'inputfieldType' => 'InputfieldRadios',
'options' => array(
'Github' => 'theme-github',
'Solarized Dark' => 'theme-solarized-dark',
),
'attributes' => array(
'description' => 'Choose the theme that should be used for the preview.',
'optionColumns' => 0,
'columnWidth' => 25,
'icon' => 'th-large',
)
),
'customCssPath' => array(
'label' => 'Custom CSS path',
'value' => null,
'inputfieldType' => 'InputfieldText',
'attributes' => array(
'icon' => 'css3',
'columnWidth' => 50,
'description' => 'Path to custom CSS file that should be used for the preview.',
'notes' => 'path should be relative to site/ - example: templates/styles/custom-tap.css',
)
),
);
protected static $allowedInputfieldTypes = array(
'InputfieldTextarea'
);
public static function getModuleInfo() {
return array(
'title' => self::TITLE,
'summary' => 'Adds a preview and zoom button to regular text areas for live preview',
'version' => 17,
'author' => 'Christian Raunitschka (owzim)',
'href' => 'https://github.com/owzim/ProcessTextareaPreview',
'autoload' => true
);
}
protected $title = self::TITLE; // to use within base process class
protected $pageName = self::PAGE_NAME; // to use within base process class
protected $url;
protected $previewStyleUrl;
protected $jsConfig;
public function __construct() {
TextareaPreviewConfigHelper::apply($this, self::$defaultConfig);
}
public function init() {
parent::init(); // required
$this->url = $this->getPageUrl(self::PAGE_NAME);
// load custom preview css if it is configured and exists
$customCssPath = $this->config->paths->site . $this->customCssPath;
$customCssUrl = $this->config->urls->site . $this->customCssPath;
if ($this->customCssPath && file_exists($customCssPath)) {
$this->previewStyleUrl = $customCssUrl;
} else {
$this->previewStyleUrl = $this->getStyleUrl($this->cssTheme);
}
$this->addHookAfter('InputfieldTextarea::render', $this, 'hook_InputfieldTextarea_render');
$this->addHookAfter('ProcessPageEdit::execute', $this, 'hook_ProcessPageEdit_execute');
$this->jsConfig = array(
'customCssPath' => $this->customCssPath,
'trackChangesInterval' => $this->trackChangesInterval
);
}
protected function hook_InputfieldTextarea_render(HookEvent $event) {
$inputfield = $event->object;
// TODO: could not yet find another way to prevent this hook from running in other places than "page edit"
if ($this->pages->get($this->input->get->id) instanceof NullPage) return;
$className = get_class($inputfield);
// currently only support for plain textarea
if(!in_array($className, self::$allowedInputfieldTypes)) return;
$fieldOutput = $event->return;
$fieldName = $inputfield->name;
$pageID = $this->input->get->id;
$urlSegements = "/{$pageID}/{$fieldName}/";
$tpl = $this->getTemplate('textarea-wrap');
$tpl->setArray(array(
'fieldOutput' => $fieldOutput,
'textareaWrapperDomID' => "wrap_Inputfield_{$fieldName}",
'textareaDomID' => "Inputfield_{$fieldName}",
'previewWrapperDomID' => "TAP-wrapper_{$fieldName}",
'iframeDomID' => "TAP-previewContent_{$fieldName}",
'params' => json_encode(array_merge($this->jsConfig, array(
'iframeUrl' => $this->getPageUrl() . self::IFRAME_URL_SEGMENT . $urlSegements,
'ajaxUrl' => $this->getPageUrl() . self::AJAX_FETCH_SEGMENT . $urlSegements,
'cookieNameText' => $this->getCookieName($fieldName, $pageID, self::COOKIE_NAME_TEXT),
'cookieNameChanged' => $this->getCookieName($fieldName, $pageID, self::COOKIE_NAME_CHANGED),
)))
));
$event->return = $tpl->render();
}
protected function hook_ProcessPageEdit_execute(HookEvent $event) {
// get the edited page
$page = $event->object->page;
// get all the fields of that page
$fields = $page->template->fieldgroup->fields;
$hasAllowedInputfieldType = false;
// loop through all fields and see if any of those are allowed
foreach ($fields as $field) {
$inputfieldType = (string) $field->type->inputfieldClass;
if(in_array($inputfieldType, self::$allowedInputfieldTypes)) {
$hasAllowedInputfieldType = true;
break;
}
}
// only then add the styles and scripts
if ($hasAllowedInputfieldType) {
$this->config->js($this->getName(), $this->jsConfig);
$this->addStyle('style');
$this->addScript('jquery.cookie');
$this->addScript('main');
}
}
public function ___executeAjaxFetch() {
$text = $this->input->text;
$pageID = $this->input->urlSegment2;
$fieldName = $this->input->urlSegment3;
$field = $this->fields->get($fieldName);
$page = $this->pages->get($pageID);
foreach($field->textformatters as $textformatter){
$tf = $this->modules->get($textformatter);
// make the textformatter aware of the page it is called from
// since $this->page or wire('page') might be used in text formatters
Wire::setFuel('page', $page);
$tf->format($text);
}
return $text;
}
public function ___executePreview() {
$pageID = $this->input->urlSegment2;
$fieldName = $this->input->urlSegment3;
foreach ($this->config->scripts->unique() as $file) {
$scripts[] = $file;
}
$scripts[] = $this->getScriptUrl('jquery.cookie');
$scripts[] = $this->getScriptUrl('main') . '?v=' . $this->getVersion();
$params = array_merge($this->jsConfig, array(
'cookieNameText' => $this->getCookieName($fieldName, $pageID, self::COOKIE_NAME_TEXT),
'cookieNameChanged' => $this->getCookieName($fieldName, $pageID, self::COOKIE_NAME_CHANGED),
));
$tpl = $this->getTemplate('iframe-content');
$tpl->setArray(array(
'styleUrl' => $this->previewStyleUrl . '?v=' . $this->getVersion(),
'scripts' => $scripts,
'params' => json_encode($params),
));
die($tpl->render());
}
public function ___install() {
$adminRootPage = $this->pages->get($this->config->adminRootPageID);
$setupPage = $adminRootPage->child('name=setup');
$page = $this->installPage($parent = $adminRootPage, $this->pageName, $this->title);
$page->addStatus(Page::statusHidden);
$page->save();
// $this->installPage($parent = $setupPage, $this->pageName, $this->title);
}
public function ___uninstall() {
$this->uninstallPages();
}
public static function getModuleConfigInputfields(array $data) {
return TextareaPreviewConfigHelper::renderForm($data, self::$defaultConfig);
}
public function getCookieName($fieldName, $pageID, $stringTemplate) {
return sprintf($stringTemplate, $this->getName(), $fieldName, $pageID);
}
protected function getName() {
return get_class($this);
}
protected function getVersion() {
$moduleInfo = self::getModuleInfo();
return $moduleInfo['version'];
}
}