add wysiwyg editor in setting.class.php

This commit is contained in:
Amberstone 2024-09-23 01:49:25 +09:00
parent 6d20404e0c
commit 90a6f8bb5e
Signed by: amber
GPG key ID: 094B0E55F98D8BF1

View file

@ -9,8 +9,10 @@
*/
class Setting
{
public static $fonts;
public $idx;
public $html;
public $extrahtml;
public $settings;
public $title;
public $cs_id;
@ -45,6 +47,7 @@ class Setting
$this->cs_name = $name;
$this->title = $title;
$this->html = "";
$this->extrahtml = "";
$this->is_value_added = false;
if ($init) {
$this->init();
@ -62,6 +65,9 @@ class Setting
case "textarea":
$this->addTextareaSetting($val["desc"], array_key_exists("default", $val) ? (is_array($val["default"]) ? $val["default"] : []) : [], array_key_exists("placeholder", $val) ? $val["placeholder"] : "");
break;
case "editor":
$this->addEditorSetting($val["desc"], array_key_exists("default", $val) ? (is_array($val["default"]) ? $val["default"] : []) : [], array_key_exists("js", $val) ? $val["js"] : true);
break;
case "image":
$this->addImageSetting($val["desc"], array_key_exists("default", $val) ? (is_array($val["default"]) ? $val["default"] : []) : []);
break;
@ -153,6 +159,7 @@ class Setting
switch ($setting["type"]) {
case "image":
case "text":
case "editor":
case "desc":
break;
case "color":
@ -197,6 +204,18 @@ class Setting
$this->is_value_added = true;
}
public function addEditorSetting($desc, $default, $js)
{
if ($this->is_value_added)
return;
$this->settings[] = [
"type" => "editor",
"html" => "<td class=\"bo-right\">{$desc}</td><td><div class=\"setter\">" . $this->editor_html($this->cs_name, "cs_value[{$this->idx}]", get_text($this->cs_value, 0)) . "</div></td></tr>"
];
$this->is_value_added = true;
$this->extrahtml = $this->get_editor_js($this->cs_name, "cs_value[{$this->idx}]");
}
public function addImageSetting($desc, $default)
{
if ($this->is_value_added)
@ -392,4 +411,73 @@ class Setting
}
return $html;
}
public function get_editor_js($id, $is_dhtml_editor = true)
{
if ($is_dhtml_editor) {
$js = "var {$id}_editor_data = oEditors.getById['{$id}'].getIR();\n";
$js .= "oEditors.getById['{$id}'].exec('UPDATE_CONTENTS_FIELD', []);\n";
$js .= "if (jQuery.inArray(document.getElementById('{$id}').value.toLowerCase().replace(/^\s*|\s*$/g, ''), ['&nbsp;','<p>&nbsp;</p>','<p><br></p>','<div><br></div>','<p></p>','<br>','']) != -1) {\n";
$js .= "document.getElementById('{$id}').value='';\n";
$js .= "}\n";
return $js;
} else {
return "var {$id}_editor = document.getElementById('{$id}');\n";
}
}
public function editor_html($id, $key, $content, $js = true, $is_dhtml_editor = true, $maxlength = 65536)
{
global $g5, $config, $w, $board, $write, $js;
if (
$is_dhtml_editor && $content &&
(
(!$w && (isset($board['bo_insert_content']) && !empty($board['bo_insert_content'])))
|| ($w == 'u' && isset($write['wr_option']) && strpos($write['wr_option'], 'html') === false)
)
) {
if (preg_match('/\r|\n/', $content) && $content === strip_tags($content, '<a><strong><b>')) {
$content = nl2br($content);
}
}
$editor_url = G5_EDITOR_URL . '/' . $config['cf_editor'];
$html = "";
$html .= "<span class=\"sound_only\">웹에디터 시작</span>";
if ($is_dhtml_editor && $js) {
$html .= "\n<script src=\"{$editor_url}/js/service/HuskyEZCreator.js\"></script>";
$html .= "\n<script> var g5_editor_url = '" . $editor_url . "', oEditors = [], ed_nonce = '" . ft_nonce_create('smarteditor') . "', editorAdditionalFontList = [";
if (!self::$fonts) {
$sql = "SELECT * FROM {$g5['font_table']} ORDER BY font_name ASC";
$result = sql_query($sql);
$addFontList = [];
while ($row = sql_fetch_array($result)) {
$font_name = addslashes($row['font_name']);
$font_family = addslashes($row['font_family']);
$addFontList[] = "['{$font_family}', '{$font_name}']";
}
if (count($addFontList) > 0) {
self::$fonts = implode(",", $addFontList);
}
}
$html .= self::$fonts;
$html .= "];</script>";
$html .= "\n<script src=\"{$editor_url}/config.js\"></script>";
$js = false;
}
$smarteditor_class = $is_dhtml_editor ? "smarteditor2" : "";
$html .= "\n<textarea id=\"{$id}\" name=\"{$key}\" class=\"{$smarteditor_class}\" maxlength=\"{$maxlength}\" style=\"width:100%;height:300px\">{$content}</textarea>";
$html .= "\n<span class=\"sound_only\">웹 에디터 끝</span>";
return $html;
}
}