|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Sergii Bondarenko, <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Drupal\TqExtension\Utils\Wysiwyg; |
|
6
|
|
|
|
|
7
|
|
|
// Contexts. |
|
8
|
|
|
use Drupal\TqExtension\Context\RawTqContext; |
|
9
|
|
|
|
|
10
|
|
|
abstract class Wysiwyg |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var RawTqContext |
|
14
|
|
|
*/ |
|
15
|
|
|
private $context; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
* JS code that return an instance of WYSIWYG editor. |
|
19
|
|
|
*/ |
|
20
|
|
|
private $object = ''; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
* Field selector. |
|
24
|
|
|
*/ |
|
25
|
|
|
private $selector = ''; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $instances = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param RawTqContext $context |
|
33
|
|
|
* Context of page. Needs to interact with browser. |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function setContext(RawTqContext $context) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->context = $context; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @see TinyMCE::__construct() |
|
42
|
|
|
* @see CKEditor::__construct() |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $javascript |
|
45
|
|
|
* Must a string of JS code that return an instance of editor. String will be |
|
46
|
|
|
* processed by sprintf() and "%s" placeholder will be replaced by field ID. |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function setObject($javascript) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->object = (string) $javascript; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $selector |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setSelector($selector) |
|
57
|
|
|
{ |
|
58
|
|
|
if (!empty($selector)) { |
|
59
|
|
|
$this->selector = (string) $selector; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getSelector() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->selector; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get the editor instance for use in Javascript. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $selector |
|
75
|
|
|
* Any selector of a form field. |
|
76
|
|
|
* |
|
77
|
|
|
* @throws \RuntimeException |
|
78
|
|
|
* @throws \Exception |
|
79
|
|
|
* @throws \WebDriver\Exception\NoSuchElement |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
* A Javascript expression that representing WYSIWYG instance. |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function getInstance($selector = '') |
|
85
|
|
|
{ |
|
86
|
|
|
if (empty($this->object)) { |
|
87
|
|
|
throw new \RuntimeException('Editor instance was not set.'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (empty($this->selector) && empty($selector)) { |
|
91
|
|
|
throw new \RuntimeException('No such editor was not selected.'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->setSelector($selector); |
|
95
|
|
|
|
|
96
|
|
|
if (empty($this->instances[$this->selector])) { |
|
97
|
|
|
$instanceId = $this->context->element('field', $this->selector)->getAttribute('id'); |
|
98
|
|
|
$instance = sprintf($this->object, $instanceId); |
|
99
|
|
|
|
|
100
|
|
|
if (!$this->context->executeJs("return !!$instance")) { |
|
101
|
|
|
throw new \Exception(sprintf('Editor "%s" was not found.', $instanceId)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->instances[$this->selector] = $instance; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->instances[$this->selector]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $method |
|
112
|
|
|
* WYSIWYG editor method. |
|
113
|
|
|
* @param string $selector |
|
114
|
|
|
* Editor selector. |
|
115
|
|
|
* @param array $arguments |
|
116
|
|
|
* Arguments for method of WYSIWYG editor. |
|
117
|
|
|
* |
|
118
|
|
|
* @throws \Exception |
|
119
|
|
|
* Throws an exception if the editor does not exist. |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
* Result of JS evaluation. |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function execute($method, $selector = '', array $arguments = []) |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->context->executeJs("return !object.$method(!args);", [ |
|
127
|
|
|
'!object' => $this->getInstance($selector), |
|
128
|
|
|
// Remove "[" character from start of the string and "]" from the end. |
|
129
|
|
|
'!args' => substr(drupal_json_encode($arguments), 1, -1), |
|
130
|
|
|
]); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $wysiwyg |
|
135
|
|
|
* @param array $arguments |
|
136
|
|
|
* |
|
137
|
|
|
* @throws \Exception |
|
138
|
|
|
* |
|
139
|
|
|
* @return self |
|
140
|
|
|
*/ |
|
141
|
|
|
public static function instantiate($wysiwyg, array $arguments = []) |
|
142
|
|
|
{ |
|
143
|
|
|
$classes = [$wysiwyg, sprintf('%s\%s', __NAMESPACE__, $wysiwyg)]; |
|
144
|
|
|
|
|
145
|
|
|
foreach ($classes as $class) { |
|
146
|
|
|
if (class_exists($class) && get_parent_class($class) == self::class) { |
|
147
|
|
|
return (new \ReflectionClass($class))->newInstanceArgs($arguments); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
throw new \Exception(sprintf( |
|
152
|
|
|
'Editor\'s object was not defined in any of these places: "%s".', |
|
153
|
|
|
implode('", "', $classes) |
|
154
|
|
|
)); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param string $text |
|
159
|
|
|
* Text to insert. |
|
160
|
|
|
* @param string $selector |
|
161
|
|
|
* Editor selector. |
|
162
|
|
|
*/ |
|
163
|
|
|
abstract public function fill($text, $selector = ''); |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param string $text |
|
167
|
|
|
* Text to insert. |
|
168
|
|
|
* @param string $selector |
|
169
|
|
|
* Editor selector. |
|
170
|
|
|
*/ |
|
171
|
|
|
abstract public function type($text, $selector = ''); |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param string $selector |
|
175
|
|
|
* Editor selector. |
|
176
|
|
|
* |
|
177
|
|
|
* @return string |
|
178
|
|
|
* Editor content. |
|
179
|
|
|
*/ |
|
180
|
|
|
abstract public function read($selector = ''); |
|
181
|
|
|
} |
|
182
|
|
|
|