1<?php
2
3/**
4 * Class CanvasElement
5 *
6 * A custom element used for DokuWiki "Igor" and later, where old JavaScript injection won't work anymore.
7 *
8 * This is not a general-purpose element. It is strictly designed for sketchcanvas plugin.
9 */
10class CanvasElement extends dokuwiki\Form\InputElement
11{
12    /**
13     * @var string the actual text within the area
14     */
15    protected $text;
16
17    /**
18     * @param string $name The name of this form element
19     * @param string $label The label text for this element
20     */
21    public function __construct()
22    {
23        parent::__construct('canvas', 'canvas', '');
24    }
25
26        /**
27     * Get or set the element's value
28     *
29     * This is the preferred way of setting the element's value
30     *
31     * @param null|string $value
32     * @return string|$this
33     */
34    public function val($value = null)
35    {
36        if ($value !== null) {
37            $this->text = $value;
38            return $this;
39        }
40        return $this->text;
41    }
42
43    /**
44     * The HTML representation of this element
45     *
46     * @return string
47     */
48    protected function mainElementHTML()
49    {
50        $escText = '"' . str_replace(array("\r", "\n"), array('\r', '\n'), addslashes($this->text)) . '"';
51
52        $attrs = buildAttributes($this->attrs());
53
54        $canvasText = <<<EOT
55<canvas id="editcanvas" $attrs></canvas>
56<script type="text/javascript"><!--
57var skcanvas;
58document.addEventListener('DOMContentLoaded', function(){
59    skcanvas = new SketchCanvas.SketchCanvas(document.getElementById('editcanvas'), {editmode: true});
60    skcanvas.loadData($escText);
61    skcanvas.onUpdateData = function(data){
62        var wikitext = document.getElementById('wiki__text');
63        wikitext.value = data;
64    }
65});
66--></script>
67<input type="button" value="Load data from text" onclick="skcanvas.loadData(document.getElementById('wiki__text').value)">
68<textarea name="wikitext" id="wiki__text" class="edit" cols="80" rows="10">$this->text</textarea>
69EOT;
70        return $canvasText;
71    }
72
73}
74