This repository was archived by the owner on Jan 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractiveNote.php
More file actions
137 lines (116 loc) · 4.28 KB
/
Copy pathInteractiveNote.php
File metadata and controls
137 lines (116 loc) · 4.28 KB
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
<?php
namespace MonkDev\InteractiveNote;
class InteractiveNote
{
const SINGLE_INPUT_REGEX = '/\{\{(.*?)\}\}/';
const FREE_FORM_INPUT_REGEX = '/\{\#\#(.*?)\#\#\}/';
protected $text;
protected $originalText;
protected $singleLineTemplate = "<input name='single-line[]' class='single-line' data-answer='__ANSWER__' type='text'>";
protected $freeFormTemplate = "<textarea name='free-form[]' class='free-form' cols='30' rows='10' data-answer='__ANSWER__'>__ANSWER__</textarea>";
protected $freeFormTextColor = 'blue';
protected $singleInputDataAnswerAttributeName = 'data-answer';
protected $inputTextColor = '#c90';
protected $correctAnswerClass = 'correct-answer';
protected $correctAnswerColor = '#090';
protected $wrongAnswerClass = 'wrong-answer';
protected $wrongAnswerColor = '#f00';
protected $autoWidth = true;
protected $disableLastPass = true;
public function __construct($text)
{
$this->originalText = $text;
}
public function setSingleInputTemplate($template)
{
$this->singleLineTemplate = $template;
}
public function setFreeFormTemplate($template)
{
$this->freeFormTemplate = $template;
}
public function setFreeFormTextColor($color)
{
$this->freeFormTextColor = $color;
}
public function setInputTextColor($color)
{
$this->inputTextColor = $color;
}
public function setCorrectAnswerColor($color)
{
$this->correctAnswerColor = $color;
}
public function setWrongAnswerColor($color)
{
$this->wrongAnswerColor = $color;
}
public function setSingleInputDataAnswerAttributeName($name)
{
$this->singleInputDataAnswerAttributeName = $name;
}
public function setCorrectAnswerClass($class)
{
$this->correctAnswerClass = $class;
}
public function setWrongAnswerClass($class)
{
$this->wrongAnswerClass = $class;
}
public function disableAutoWidth()
{
$this->autoWidth = false;
return $this;
}
public function enableLastPass()
{
$this->disableLastPass = false;
return $this;
}
public function parse()
{
$this->convertSingleLineInputs()
->convertFreeFormInputs();
return $this->text;
}
protected function convertSingleLineInputs()
{
$this->text = preg_replace_callback(self::SINGLE_INPUT_REGEX, function ($match) {
$answer = htmlspecialchars(trim($match[1]), ENT_QUOTES | ENT_HTML5);
$input = str_replace('__ANSWER__', $answer, $this->singleLineTemplate);
if ($this->autoWidth) {
$width = strlen($answer) * 0.7;
$input = str_replace('<input', "<input style='width:{$width}em;' ", $input);
}
if ($this->disableLastPass) {
$input = str_replace('<input', "<input data-lpignore='true' ", $input);
}
return $input;
}, $this->originalText);
return $this;
}
protected function convertFreeFormInputs()
{
$this->text = preg_replace_callback(self::FREE_FORM_INPUT_REGEX, function ($match) {
$answer = htmlspecialchars(trim($match[1]), ENT_QUOTES|ENT_HTML5);
return str_replace('__ANSWER__', $answer, $this->freeFormTemplate);
}, $this->text);
}
public function getJavascriptSnippet()
{
$js = file_get_contents(__DIR__ . '/assets/base.js');
$js = str_replace('__SINGLE-INPUT-DATA-ANSWER-ATTRIBUTE-NAME__', $this->singleInputDataAnswerAttributeName, $js);
$js = str_replace('__CORRECT_ANSWER_CLASS__', $this->correctAnswerClass, $js);
$js = str_replace('__WRONG_ANSWER_CLASS__', $this->wrongAnswerClass, $js);
return "<script>{$js}</script>";
}
public function getCssSnippet()
{
$css = file_get_contents(__DIR__ . '/assets/base.css');
$css = str_replace('__FREE_FORM_TEXT_COLOR__', $this->freeFormTextColor, $css);
$css = str_replace('__INPUT_COLOR__', $this->inputTextColor, $css);
$css = str_replace('__CORRECT_ANSWER_COLOR__', $this->correctAnswerColor, $css);
$css = str_replace('__WRONG_ANSWER_COLOR__', $this->wrongAnswerColor, $css);
return "<style id='notes-css'>{$css}</style>";
}
}