Skip to content

Commit

Permalink
Fix canvas editing GUI didn't work for Igor and later
Browse files Browse the repository at this point in the history
  • Loading branch information
msakuta committed Jan 7, 2024
1 parent 3b98294 commit b8628df
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
73 changes: 73 additions & 0 deletions CanvasElement.php
@@ -0,0 +1,73 @@
<?php

/**
* Class CanvasElement
*
* A custom element used for DokuWiki "Igor" and later, where old JavaScript injection won't work anymore.
*
* This is not a general-purpose element. It is strictly designed for sketchcanvas plugin.
*/
class CanvasElement extends dokuwiki\Form\InputElement
{
/**
* @var string the actual text within the area
*/
protected $text;

/**
* @param string $name The name of this form element
* @param string $label The label text for this element
*/
public function __construct()
{
parent::__construct('canvas', 'canvas', '');
}

/**
* Get or set the element's value
*
* This is the preferred way of setting the element's value
*
* @param null|string $value
* @return string|$this
*/
public function val($value = null)
{
if ($value !== null) {
$this->text = $value;
return $this;
}
return $this->text;
}

/**
* The HTML representation of this element
*
* @return string
*/
protected function mainElementHTML()
{
$escText = '"' . str_replace(array("\r", "\n"), array('\r', '\n'), addslashes($this->text)) . '"';

$attrs = buildAttributes($this->attrs());

$canvasText = <<<EOT
<canvas id="editcanvas" $attrs></canvas>
<script type="text/javascript"><!--
var skcanvas;
document.addEventListener('DOMContentLoaded', function(){
skcanvas = new SketchCanvas(document.getElementById('editcanvas'), {editmode: true});
skcanvas.loadData($escText);
skcanvas.onUpdateData = function(data){
var wikitext = document.getElementById('wiki__text');
wikitext.value = data;
}
});
--></script>
<input type="button" value="Load data from text" onclick="skcanvas.loadData(document.getElementById('wiki__text').value)">
<textarea name="wikitext" id="wiki__text" class="edit" cols="80" rows="10">$this->text</textarea>
EOT;
return $canvasText;
}

}
30 changes: 30 additions & 0 deletions action.php
Expand Up @@ -9,6 +9,8 @@
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();

require_once("CanvasElement.php");

/**
* Add scripts via an event handler
*/
Expand All @@ -22,6 +24,8 @@ public function register(Doku_Event_Handler $controller) {

$controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'editButton');
$controller->register_hook('HTML_EDIT_FORMSELECTION', 'BEFORE', $this, 'editForm');
// After Igor
$controller->register_hook('EDIT_FORM_ADDTEXTAREA', 'BEFORE', $this, 'editFormNew');
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_newfigure');

$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'toolbarDefine');
Expand Down Expand Up @@ -121,6 +125,32 @@ public function editForm(Doku_Event $event, $param){
}
}

/**
* A
*
* @param Doku_Event $event
*/
public function editFormNew(Doku_Event $event, $param){
global $TEXT;
if($event->data['target'] !== 'plugin_sketchcanvas')
return;
$event->preventDefault();

$event->data['media_manager'] = false;

$form =& $event->data['form'];
$canvasElem = new CanvasElement();
$canvasElem->val($TEXT);
$form->addElement($canvasElem);

// Pass wikitext through POSTs for previewing and saving
if(isset($_POST['editfigure__new'])) {
foreach($_POST['editfigure__new'] as $k => $v) {
$form->addHidden("editfigure__new[$k]", $v);
}
}
}

/**
* Add a toolbar button to add a new figure
*
Expand Down

0 comments on commit b8628df

Please sign in to comment.