-
Notifications
You must be signed in to change notification settings - Fork 24
/
qa-markdown-editor.php
150 lines (127 loc) · 4.38 KB
/
qa-markdown-editor.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*
Question2Answer Markdown editor plugin
License: http://www.gnu.org/licenses/gpl.html
*/
class qa_markdown_editor
{
private $pluginurl;
private $cssopt = 'markdown_editor_css';
private $convopt = 'markdown_comment';
private $hljsopt = 'markdown_highlightjs';
private $impuplopt = 'markdown_uploadimage';
public function load_module($directory, $urltoroot)
{
$this->pluginurl = $urltoroot;
}
public function calc_quality($content, $format)
{
return $format == 'markdown' ? 1.0 : 0.8;
}
public function option_default($opt)
{
$defaults = [
$this->cssopt => 0,
$this->convopt => 1,
$this->hljsopt => 0,
$this->impuplopt => 0,
];
if (isset($defaults[$opt])) {
return $defaults[$opt];
}
}
public function get_field(&$qa_content, $content, $format, $fieldname, $rows, $autofocus)
{
$html = '<div id="wmd-button-bar-'.$fieldname.'" class="wmd-button-bar"></div>' . "\n";
$html .= '<textarea name="'.$fieldname.'" id="wmd-input-'.$fieldname.'" class="wmd-input">'.$content.'</textarea>' . "\n";
$html .= '<h3>'.qa_lang_html('markdown/preview').'</h3>' . "\n";
$html .= '<div id="wmd-preview-'.$fieldname.'" class="wmd-preview"></div>' . "\n";
$imageUploadUrl = qa_js(qa_path('qa-markdown-upload'));
$uploadimg = (int) qa_opt($this->impuplopt);
$html .= "<script>var image_upload_path=$imageUploadUrl; var image_upload_enabled=$uploadimg;</script>";
// $html .= '<script src="'.$this->pluginurl.'pagedown/Markdown.Converter.js"></script>' . "\n";
// $html .= '<script src="'.$this->pluginurl.'pagedown/Markdown.Sanitizer.js"></script>' . "\n";
// $html .= '<script src="'.$this->pluginurl.'pagedown/Markdown.Editor.js"></script>' . "\n";
// comment this script and uncomment the 3 above to use the non-minified code
$html .= '<script src="'.$this->pluginurl.'pagedown/markdown.min.js?v=2.6.2"></script>' . "\n";
return array('type'=>'custom', 'html'=>$html);
}
public function read_post($fieldname)
{
$html = $this->_my_qa_post_text($fieldname);
return array(
'format' => 'markdown',
'content' => $html
);
}
public function load_script($fieldname)
{
return
'var converter = Markdown.getSanitizingConverter();' . "\n" .
'var editor = new Markdown.Editor(converter, "-'.$fieldname.'");' . "\n" .
'editor.run();' . "\n";
}
// set admin options
public function admin_form(&$qa_content)
{
$saved_msg = null;
if (qa_clicked('markdown_save')) {
// save options
$hidecss = qa_post_text('md_hidecss') ? '1' : '0';
qa_opt($this->cssopt, $hidecss);
$convert = qa_post_text('md_comments') ? '1' : '0';
qa_opt($this->convopt, $convert);
$convert = qa_post_text('md_highlightjs') ? '1' : '0';
qa_opt($this->hljsopt, $convert);
$convert = qa_post_text('md_uploadimage') ? '1' : '0';
qa_opt($this->impuplopt, $convert);
$saved_msg = qa_lang_html('admin/options_saved');
}
return array(
'ok' => $saved_msg,
'style' => 'wide',
'fields' => array(
'css' => array(
'type' => 'checkbox',
'label' => qa_lang_html('markdown/admin_hidecss'),
'tags' => 'NAME="md_hidecss"',
'value' => qa_opt($this->cssopt) === '1',
'note' => qa_lang_html('markdown/admin_hidecss_note'),
),
'comments' => array(
'type' => 'checkbox',
'label' => qa_lang_html('markdown/admin_comments'),
'tags' => 'NAME="md_comments"',
'value' => qa_opt($this->convopt) === '1',
'note' => qa_lang_html('markdown/admin_comments_note'),
),
'highlightjs' => array(
'type' => 'checkbox',
'label' => qa_lang_html('markdown/admin_syntax'),
'tags' => 'NAME="md_highlightjs"',
'value' => qa_opt($this->hljsopt) === '1',
'note' => qa_lang_html('markdown/admin_syntax_note'),
),
'uploadimage' => array(
'type' => 'checkbox',
'label' => qa_lang_html('markdown/admin_image'),
'tags' => 'NAME="md_uploadimage"',
'value' => qa_opt($this->impuplopt) === '1',
'note' => qa_lang_html('markdown/admin_image_note'),
)
),
'buttons' => array(
'save' => array(
'tags' => 'NAME="markdown_save"',
'label' => qa_lang_html('admin/save_options_button'),
'value' => '1',
),
),
);
}
// copy of qa-base.php > qa_post_text, with trim() function removed.
private function _my_qa_post_text($field)
{
return isset($_POST[$field]) ? preg_replace('/\r\n?/', "\n", qa_gpc_to_string($_POST[$field])) : null;
}
}