forked from cosmocode/edittable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
229 lines (198 loc) · 8.35 KB
/
action.php
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
<?php
/**
* Table editor
*
* @author Adrian Lang <[email protected]>
*/
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once DOKU_PLUGIN.'action.php';
require_once DOKU_PLUGIN.'edittable/common.php';
class action_plugin_edittable extends DokuWiki_Action_Plugin {
/**
* Register its handlers with the DokuWiki's event controller
*/
function register(&$controller) {
$controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'html_secedit_button');
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_table_post');
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_newtable');
$controller->register_hook('HTML_EDIT_FORMSELECTION', 'BEFORE', $this, 'html_table_editform');
$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'toolbar');
$controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'jump_to_section');
}
function getLang($id) {
$r = parent::getLang($id);
if ($r !== '') return $r;
$js = parent::getLang('js');
return $js[$id];
}
function toolbar(&$event) {
$menu = array(
array('title' => $this->getLang('toggle_header'),
'key' => 'H',
'icon' => 'text_heading.png',
'type' => 'toggletag'),
array('title' => $this->getLang('val_align_left'),
'key' => 'N',
'icon' => 'a_left.png',
'type' => 'val',
'prop' => 'align',
'class' => 'separator',
'val' => 'left'),
array('title' => $this->getLang('val_align_center'),
'key' => 'M',
'icon' => 'a_center.png',
'type' => 'val',
'prop' => 'align',
'val' => 'center'),
array('title' => $this->getLang('val_align_right'),
'key' => 'R',
'icon' => 'a_right.png',
'type' => 'val',
'prop' => 'align',
'val' => 'right'),
array('title' => $this->getLang('span_col_plus'),
'icon' => 'merge_right.png',
'type' => 'span',
'class' => 'separator',
'target' => 'col',
'ops' => '+'),
array('title' => $this->getLang('span_col_minus'),
'icon' => 'split_right.png',
'type' => 'span',
'target' => 'col',
'ops' => '-'),
array('title' => $this->getLang('span_row_plus'),
'icon' => 'merge_down.png',
'type' => 'span',
'class' => 'separator',
'target' => 'row',
'ops' => '+'),
array('title' => $this->getLang('span_row_minus'),
'icon' => 'split_down.png',
'type' => 'span',
'target' => 'row',
'ops' => '-'),
array('title' => $this->getLang('struct_row_plus'),
'icon' => 'row_insert.png',
'type' => 'structure',
'class' => 'separator',
'target' => 'row',
'ops' => '+'),
array('title' => $this->getLang('struct_row_minus'),
'icon' => 'row_delete.png',
'type' => 'structure',
'target' => 'row',
'ops' => '-'),
array('title' => $this->getLang('struct_col_plus'),
'icon' => 'column_add.png',
'type' => 'structure',
'class' => 'separator',
'target' => 'col',
'ops' => '+'),
array('title' => $this->getLang('struct_col_minus'),
'icon' => 'column_delete.png',
'type' => 'structure',
'target' => 'col',
'ops' => '-'),
);
foreach ($menu as &$entry) {
$entry['icon'] = '../../plugins/edittable/images/' . $entry['icon'];
}
// use JSON to build the JavaScript array
$json = new JSON();
echo 'var table_toolbar = '.$json->encode($menu).';'.DOKU_LF;
$event->data[] = array('title' => $this->getLang('add_table'),
'type' => 'insertTable',
'icon' => '../../plugins/edittable/images/add_table.png');
}
/**
* Handles a POST from the table editor
*
* This function preprocesses a POST from the table editor. It converts the
* table array to plain wiki markup text and stores it in the global $TEXT.
*
* @author Adrian Lang <[email protected]>
*/
function handle_table_post($event) {
if (!isset($_POST['table'])) {
return;
}
global $TEXT;
$TEXT = table_to_wikitext($_POST['table']);
}
function handle_newtable($event) {
if (!isset($_POST['edittable__new'])) {
return;
}
foreach($_POST['edittable__new'] as &$v) {
// Form performs a formText
$v = cleanText($v);
}
global $TEXT;
if (isset($_POST['do']['edit'])) {
$_REQUEST['target'] = 'table';
$TEXT = "^ ^ ^\n";
foreach (explode("\n", $_POST['edittable__new']['text']) as $line) {
$TEXT .= "| $line | |\n";
}
} elseif (isset($_POST['do']['draftdel'])) {
$TEXT = $_POST['edittable__new']['pre'] .
$_POST['edittable__new']['text'] .
$_POST['edittable__new']['suf'];
global $ACT;
$ACT = 'edit';
$_REQUEST['target'] = 'section';
} elseif (isset($_POST['do']['save'])) {
$TEXT = $_POST['edittable__new']['pre'] .
$TEXT .
$_POST['edittable__new']['suf'];
global $ACT;
$ACT = 'edit';
$_REQUEST['target'] = 'section';
}
}
function html_secedit_button(&$event) {
if ($event->data['target'] !== 'table') {
return;
}
$event->data['name'] = $this->getLang('secedit_name');
}
function html_table_editform($event) {
global $TEXT;
if ($event->data['target'] !== 'table') {
// Not a table edit
return;
}
$event->stopPropagation();
$event->preventDefault();
require_once 'renderer_table_edit.php';
$Renderer = new Doku_Renderer_xhtml_table_edit();
$instructions = p_get_instructions($TEXT);
// Loop through the instructions
foreach ( $instructions as $instruction ) {
// Execute the callback against the Renderer
call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
}
$event->data['form']->addElement('<div id="edit__wrap">' .$Renderer->doc .'</div>');
if (isset($_POST['edittable__new'])) {
foreach($_POST['edittable__new'] as $k => $v) {
$event->data['form']->addHidden("edittable__new[$k]", $v);
}
}
}
/**
* Jump after save to the section containing this table
*/
function jump_to_section(&$event) {
if (!isset($_POST['table'])) {
return;
}
global $PRE;
if(preg_match_all('/^\s*={2,}([^=\n]+)/m',$PRE,$match, PREG_SET_ORDER)) {
$check = false; //Byref
$match = array_pop($match);
$event->data['fragment'] = sectionID($match[1], $check);
}
}
}