Skip to content

Commit 6eb2267

Browse files
authored
Merge pull request #198 from ssahara/form
support new EDIT_FORM_ADDTEXTAREA event
2 parents 88e027f + 110a8e7 commit 6eb2267

File tree

8 files changed

+131
-86
lines changed

8 files changed

+131
-86
lines changed

_test/renderer_plugin_edittable_json.test.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ function test_table() {
5252
);
5353

5454
$renderer = $this->render($input);
55-
$json = new JSON(JSON_LOOSE_TYPE);
5655

57-
$this->assertEquals($data, $json->decode($renderer->getDataJSON()));
58-
$this->assertEquals($meta, $json->decode($renderer->getMetaJSON()));
56+
$this->assertEquals($data, json_decode($renderer->getDataJSON(), true));
57+
$this->assertEquals($meta, json_decode($renderer->getMetaJSON(), true));
5958
}
6059

6160

action/editor.php

Lines changed: 91 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,26 @@
66
* @author Andreas Gohr <[email protected]>
77
*/
88

9-
if(!defined('DOKU_INC')) die();
10-
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
9+
use dokuwiki\Form\Form;
10+
use dokuwiki\Utf8;
1111

1212
/**
1313
* handles all the editor related things
1414
*
1515
* like displaying the editor and adding custom edit buttons
1616
*/
17-
class action_plugin_edittable_editor extends DokuWiki_Action_Plugin {
18-
17+
class action_plugin_edittable_editor extends DokuWiki_Action_Plugin
18+
{
1919
/**
2020
* Register its handlers with the DokuWiki's event controller
2121
*/
22-
function register(Doku_Event_Handler $controller) {
22+
public function register(Doku_Event_Handler $controller)
23+
{
2324
// register custom edit buttons
2425
$controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'secedit_button');
2526

2627
// register our editor
28+
$controller->register_hook('EDIT_FORM_ADDTEXTAREA', 'BEFORE', $this, 'editform');
2729
$controller->register_hook('HTML_EDIT_FORMSELECTION', 'BEFORE', $this, 'editform');
2830

2931
// register preprocessing for accepting editor data
@@ -38,10 +40,10 @@ function register(Doku_Event_Handler $controller) {
3840
*
3941
* @param Doku_Event $event
4042
*/
41-
function secedit_button(Doku_Event $event) {
42-
if($event->data['target'] !== 'table') {
43-
return;
44-
}
43+
public function secedit_button(Doku_Event $event)
44+
{
45+
if ($event->data['target'] !== 'table') return;
46+
4547
$event->data['name'] = $this->getLang('secedit_name');
4648
}
4749

@@ -50,12 +52,14 @@ function secedit_button(Doku_Event $event) {
5052
*
5153
* @param Doku_Event $event
5254
*/
53-
function editform(Doku_Event $event) {
55+
public function editform(Doku_Event $event)
56+
{
5457
global $TEXT;
5558
global $RANGE;
59+
global $INPUT;
5660

57-
if($event->data['target'] !== 'table') return;
58-
if(!$RANGE){
61+
if ($event->data['target'] !== 'table') return;
62+
if (!$RANGE){
5963
// section editing failed, use default editor instead
6064
$event->data['target'] = 'section';
6165
return;
@@ -69,7 +73,7 @@ function editform(Doku_Event $event) {
6973
$instructions = p_get_instructions($TEXT);
7074

7175
// Loop through the instructions
72-
foreach($instructions as $instruction) {
76+
foreach ($instructions as $instruction) {
7377
// Execute the callback against the Renderer
7478
call_user_func_array(array(&$Renderer, $instruction[0]), $instruction[1]);
7579
}
@@ -79,21 +83,36 @@ function editform(Doku_Event $event) {
7983
/** @var Doku_Form $form */
8084
$form =& $event->data['form'];
8185

82-
// data for handsontable
83-
$form->addHidden('edittable_data', $Renderer->getDataJSON());
84-
$form->addHidden('edittable_meta', $Renderer->getMetaJSON());
85-
$form->addElement('<div id="edittable__editor"></div>');
86+
if (is_a($form, Form::class)) { // $event->name is EDIT_FORM_ADDTEXTAREA
87+
// data for handsontable
88+
$form->setHiddenField('edittable_data', $Renderer->getDataJSON());
89+
$form->setHiddenField('edittable_meta', $Renderer->getMetaJSON());
90+
$form->addHTML('<div id="edittable__editor"></div>');
91+
92+
// set data from action asigned to "New Table" button in the toolbar
93+
foreach ($INPUT->post->arr('edittable__new', []) as $k => $v) {
94+
$form->setHiddenField("edittable__new[$k]", $v);
95+
}
96+
97+
// set target and range to keep track during previews
98+
$form->setHiddenField('target', 'table');
99+
$form->setHiddenField('range', $RANGE);
100+
101+
} else { // $event->name is HTML_EDIT_FORMSELECTION
102+
// data for handsontable
103+
$form->addHidden('edittable_data', $Renderer->getDataJSON());
104+
$form->addHidden('edittable_meta', $Renderer->getMetaJSON());
105+
$form->addElement('<div id="edittable__editor"></div>');
86106

87-
// FIXME add explanation here
88-
if(isset($_POST['edittable__new'])) {
89-
foreach($_POST['edittable__new'] as $k => $v) {
107+
// set data from action asigned to "New Table" button in the toolbar
108+
foreach ($INPUT->post->arr('edittable__new', []) as $k => $v) {
90109
$form->addHidden("edittable__new[$k]", $v);
91110
}
92-
}
93111

94-
// set target and range to keep track during previews
95-
$form->addHidden('target', 'table');
96-
$form->addHidden('range', $RANGE);
112+
// set target and range to keep track during previews
113+
$form->addHidden('target', 'table');
114+
$form->addHidden('range', $RANGE);
115+
}
97116
}
98117

99118
/**
@@ -103,14 +122,14 @@ function editform(Doku_Event $event) {
103122
*
104123
* @author Andreas Gohr <gohr@cosmocode,de>
105124
*/
106-
public function handle_table_post($event) {
125+
public function handle_table_post(Doku_Event $event)
126+
{
107127
global $TEXT;
108128
global $INPUT;
109-
if(!$INPUT->post->has('edittable_data')) return;
129+
if (!$INPUT->post->has('edittable_data')) return;
110130

111-
$json = new JSON(JSON_LOOSE_TYPE);
112-
$data = $json->decode($INPUT->post->str('edittable_data'));
113-
$meta = $json->decode($INPUT->post->str('edittable_meta'));
131+
$data = json_decode($INPUT->post->str('edittable_data'), true);
132+
$meta = json_decode($INPUT->post->str('edittable_meta'), true);
114133

115134
$TEXT = $this->build_table($data, $meta);
116135
}
@@ -124,20 +143,21 @@ public function handle_table_post($event) {
124143
* @param array $meta meta data for each cell
125144
* @return string
126145
*/
127-
public function build_table($data, $meta) {
146+
public function build_table($data, $meta)
147+
{
128148
$table = '';
129149
$rows = count($data);
130150
$cols = $rows ? count($data[0]) : 0;
131151

132152
$colmax = $cols ? array_fill(0, $cols, 0) : array();
133153

134154
// find maximum column widths
135-
for($row = 0; $row < $rows; $row++) {
136-
for($col = 0; $col < $cols; $col++) {
137-
$len = utf8_strlen($data[$row][$col]);
155+
for ($row = 0; $row < $rows; $row++) {
156+
for ($col = 0; $col < $cols; $col++) {
157+
$len = $this->strWidth($data[$row][$col]);
138158

139159
// alignment adds padding
140-
if($meta[$row][$col]['align'] == 'center') {
160+
if ($meta[$row][$col]['align'] == 'center') {
141161
$len += 4;
142162
} else {
143163
$len += 3;
@@ -146,19 +166,19 @@ public function build_table($data, $meta) {
146166
// remember lenght
147167
$meta[$row][$col]['length'] = $len;
148168

149-
if($len > $colmax[$col]) $colmax[$col] = $len;
169+
if ($len > $colmax[$col]) $colmax[$col] = $len;
150170
}
151171
}
152172

153173
$last = '|'; // used to close the last cell
154-
for($row = 0; $row < $rows; $row++) {
155-
for($col = 0; $col < $cols; $col++) {
174+
for ($row = 0; $row < $rows; $row++) {
175+
for ($col = 0; $col < $cols; $col++) {
156176

157177
// minimum padding according to alignment
158-
if($meta[$row][$col]['align'] == 'center') {
178+
if ($meta[$row][$col]['align'] == 'center') {
159179
$lpad = 2;
160180
$rpad = 2;
161-
} elseif($meta[$row][$col]['align'] == 'right') {
181+
} elseif ($meta[$row][$col]['align'] == 'right') {
162182
$lpad = 2;
163183
$rpad = 1;
164184
} else {
@@ -170,13 +190,13 @@ public function build_table($data, $meta) {
170190
$target = $colmax[$col];
171191

172192
// colspanned columns span all the cells
173-
for($i = 1; $i < $meta[$row][$col]['colspan']; $i++) {
193+
for ($i = 1; $i < $meta[$row][$col]['colspan']; $i++) {
174194
$target += $colmax[$col + $i];
175195
}
176196

177197
// copy colspans to rowspans below if any
178-
if($meta[$row][$col]['colspan'] > 1){
179-
for($i = 1; $i < $meta[$row][$col]['rowspan']; $i++) {
198+
if ($meta[$row][$col]['colspan'] > 1) {
199+
for ($i = 1; $i < $meta[$row][$col]['rowspan']; $i++) {
180200
$meta[$row + $i][$col]['colspan'] = $meta[$row][$col]['colspan'];
181201
}
182202
}
@@ -186,15 +206,15 @@ public function build_table($data, $meta) {
186206
$addpad = $target - $length;
187207

188208
// decide which side needs padding
189-
if($meta[$row][$col]['align'] == 'right') {
209+
if ($meta[$row][$col]['align'] == 'right') {
190210
$lpad += $addpad;
191211
} else {
192212
$rpad += $addpad;
193213
}
194214

195215
// add the padding
196216
$cdata = $data[$row][$col];
197-
if(!$meta[$row][$col]['hide'] || $cdata) {
217+
if (!$meta[$row][$col]['hide'] || $cdata) {
198218
$cdata = str_pad('', $lpad).$cdata.str_pad('', $rpad);
199219
}
200220

@@ -212,4 +232,31 @@ public function build_table($data, $meta) {
212232
return $table;
213233
}
214234

235+
/**
236+
* Return width of string
237+
*
238+
* @param string $str
239+
* @return int
240+
*/
241+
public function strWidth($str)
242+
{
243+
static $callable;
244+
245+
if (isset($callable)) {
246+
return $callable($str);
247+
} else {
248+
if (UTF8_MBSTRING) {
249+
// count fullwidth characters as 2, halfwidth characters as 1
250+
$callable = 'mb_strwidth';
251+
} elseif (method_exists(Utf8\PhpString::class, 'strlen')) {
252+
// count any characters as 1
253+
$callable = [Utf8\PhpString::class, 'strlen'];
254+
} else {
255+
// fallback deprecated utf8_strlen since 2019-06-09
256+
$callable = 'utf8_strlen';
257+
}
258+
return $this->strWidth($str);
259+
}
260+
}
261+
215262
}

action/jsinfo.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
<?php
22

3-
if(!defined('DOKU_INC')) die();
4-
53
/**
64
* handles the data that has to be written into jsinfo
75
*
86
* like displaying the editor and adding custom edit buttons
97
*/
10-
class action_plugin_edittable_jsinfo extends DokuWiki_Action_Plugin {
11-
8+
class action_plugin_edittable_jsinfo extends DokuWiki_Action_Plugin
9+
{
1210
/**
1311
* Register its handlers with the DokuWiki's event controller
1412
*/
15-
function register(Doku_Event_Handler $controller) {
13+
public function register(Doku_Event_Handler $controller)
14+
{
1615
// register custom edit buttons
1716
$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'fill_jsinfo');
1817
}
1918

20-
function fill_jsinfo() {
19+
public function fill_jsinfo()
20+
{
2121
global $JSINFO;
2222
$JSINFO['plugins']['edittable']['default columnwidth'] = $this->getConf('default colwidth');
2323
}

action/newtable.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55
* @author Adrian Lang <[email protected]>
66
*/
77

8-
if(!defined('DOKU_INC')) die();
9-
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
10-
118
/**
129
* Handles the inserting of a new table in a running edit session
1310
*/
14-
class action_plugin_edittable_newtable extends DokuWiki_Action_Plugin {
15-
11+
class action_plugin_edittable_newtable extends DokuWiki_Action_Plugin
12+
{
1613
/**
1714
* Register its handlers with the DokuWiki's event controller
1815
*/
19-
function register(Doku_Event_Handler $controller) {
16+
function register(Doku_Event_Handler $controller)
17+
{
2018
$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'toolbar');
2119

2220
//$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_newtable');
@@ -28,7 +26,8 @@ function register(Doku_Event_Handler $controller) {
2826
*
2927
* @param Doku_Event $event
3028
*/
31-
function toolbar($event) {
29+
public function toolbar(Doku_Event $event)
30+
{
3231
$event->data[] = array(
3332
'title' => $this->getLang('add_table'),
3433
'type' => 'NewTable',
@@ -42,11 +41,12 @@ function toolbar($event) {
4241
*
4342
* @param Doku_Event $event
4443
*/
45-
function handle_newtable($event) {
44+
public function handle_newtable(Doku_Event $event)
45+
{
4646
global $INPUT;
4747
global $TEXT;
4848

49-
if(!$INPUT->post->has('edittable__new')) return;
49+
if (!$INPUT->post->has('edittable__new')) return;
5050

5151
/*
5252
* $fields['pre'] has all data before the selection when the "Insert table" button was clicked
@@ -64,7 +64,7 @@ function handle_newtable($event) {
6464

6565

6666
$event->data = act_clean($event->data);
67-
switch($event->data){
67+
switch ($event->data) {
6868
case 'preview':
6969
// preview view of a table edit
7070
$INPUT->post->set('target', 'table');
@@ -73,7 +73,7 @@ function handle_newtable($event) {
7373
// edit view of a table (first edit)
7474
$INPUT->post->set('target', 'table');
7575
$TEXT = "^ ^ ^\n";
76-
foreach(explode("\n", $fields['text']) as $line) {
76+
foreach (explode("\n", $fields['text']) as $line) {
7777
$TEXT .= "| $line | |\n";
7878
}
7979
break;

0 commit comments

Comments
 (0)