• Модуль: fileman
  • Путь к файлу: ~/bitrix/modules/fileman/classes/general/html_editor.php
  • Класс: CHTMLEditor
  • Вызов: CHTMLEditor::Init
private function Init($arParams)
{
	global $USER;
	?>
	id = (isset($arParams['id']) && $arParams['id'] <> '') ? $arParams['id'] : 'bxeditor'.mb_substr(uniqid(mt_rand(), true), 0, 4);
	$this->id = preg_replace("/[^a-zA-Z0-9_:.]/is", "", $this->id);
	if (isset($arParams['name']))
	{
		$this->name = preg_replace("/[^a-zA-Z0-9_:.]/is", "", $arParams['name']);
	}
	else
	{
		$this->name = $this->id;
	}

	$this->cssIframePath = $this->GetActualPath($basePath.'iframe-style.css');

	CJSCore::RegisterExt('html_editor', array(
		'js' => array(
			$basePath.'range.js',
			$basePath.'html-actions.js',
			$basePath.'html-views.js',
			$basePath.'html-parser.js',
			$basePath.'html-base-controls.js',
			$basePath.'html-controls.js',
			$basePath.'html-components.js',
			$basePath.'html-snippets.js',
			$basePath.'html-editor.js',
			'/bitrix/js/main/dd.js'
		),
		'css' => $basePath.'html-editor.css',
		'rel' => array('ui.design-tokens', 'date', 'timer')
	));
	CUtil::InitJSCore(array('html_editor'));

	BitrixMainUIExtension::load(['ajax']);

	foreach(GetModuleEvents("fileman", "OnBeforeHTMLEditorScriptRuns", true) as $arEvent)
		ExecuteModuleEventEx($arEvent);

	$this->bAutorized = is_object($USER) && $USER->IsAuthorized();
	if (isset($arParams['allowPhp']) && !isset($arParams['bAllowPhp']))
	{
		$arParams['bAllowPhp'] = $arParams['allowPhp'];
	}

	$this->bAllowPhp = $arParams['bAllowPhp'] !== false;

	$arParams['limitPhpAccess'] = $arParams['limitPhpAccess'] === true;
	$this->display = !isset($arParams['display']) || $arParams['display'];

	$arParams["bodyClass"] = COption::GetOptionString("fileman", "editor_body_class", "");
	$arParams["bodyId"] = COption::GetOptionString("fileman", "editor_body_id", "");

	$this->content = ($arParams['content'] ?? '');
	$this->content = preg_replace("/rn/is", "n", $this->content);

	$this->inputName = isset($arParams['inputName']) ? $arParams['inputName'] : $this->name;
	$this->inputId = isset($arParams['inputId']) ? $arParams['inputId'] : 'html_editor_content_id';

	$arParams["bbCode"] = (isset($arParams["bbCode"]) && $arParams["bbCode"]) || (isset($arParams["BBCode"]) && $arParams["BBCode"]);

	// Site id
	if (!isset($arParams['siteId']))
	{
		$siteId = CSite::GetDefSite();
	}
	else
	{
		$siteId = $arParams['siteId'];
		$res = CSite::GetByID($siteId);
		if (!$res->Fetch())
		{
			$siteId = CSite::GetDefSite();
		}
	}

	if (!isset($siteId) && defined(SITE_ID))
	{
		$siteId = SITE_ID;
		$res = CSite::GetByID($siteId);
		if (!$res->Fetch())
		{
			$siteId = CSite::GetDefSite();
		}
	}

	$templateId = null;
	if (isset($arParams['templateId']))
	{
		$templateId = $arParams['templateId'];
	}
	elseif (defined('SITE_TEMPLATE_ID'))
	{
		$templateId = SITE_TEMPLATE_ID;
	}

	if (!isset($templateId) && isset($_GET['siteTemplateId']))
	{
		$templateId = $_GET['siteTemplateId'];
	}

	if ($arParams["bbCode"])
	{
		$arTemplates = array();
		$arSnippets = array();
		$templateParams = array();
	}
	else
	{
		if (isset($arParams['arTemplates']))
		{
			$arTemplates = $arParams['arTemplates'];
		}
		else
		{
			$arTemplates = self::GetSiteTemplates();
		}

		if (!isset($templateId) && isset($siteId))
		{
			$dbSiteRes = CSite::GetTemplateList($siteId);
			$first = false;
			while($arSiteRes = $dbSiteRes->Fetch())
			{
				if (!$first)
				{
					$first = $arSiteRes['TEMPLATE'];
				}
				if ($arSiteRes['CONDITION'] == "")
				{
					$templateId = $arSiteRes['TEMPLATE'];
					break;
				}
			}

			if (!isset($templateId))
			{
				$templateId = $first ? $first : '';
			}
		}

		$arSnippets = array($templateId => self::GetSnippets($templateId));
		$templateParams = self::GetSiteTemplateParams($templateId, $siteId);
	}

	$userSettings = array(
		'view' => isset($arParams["view"]) ? $arParams["view"] : 'wysiwyg',
		'split_vertical' => 0,
		'split_ratio' => 1,
		'taskbar_shown' => 0,
		'taskbar_width' => 250,
		'specialchars' => false,
		'clean_empty_spans' => 'Y',
		'paste_clear_colors' => 'Y',
		'paste_clear_borders' => 'Y',
		'paste_clear_decor' => 'Y',
		'paste_clear_table_dimen' => 'Y',
		'show_snippets' => 'Y',
		'link_dialog_type' => 'internal'
	);

	$settingsKey = self::GetSettingKey($arParams);
	$curSettings = CUserOptions::GetOption("html_editor", $settingsKey, false, $USER->GetId());
	if (is_array($curSettings))
	{
		foreach ($userSettings as $k => $val)
		{
			if (isset($curSettings[$k]))
			{
				$userSettings[$k] = $curSettings[$k];
			}
		}
	}

	if(!isset($arParams["uploadImagesFromClipboard"]) && $arParams["bbCode"])
	{
		$arParams["uploadImagesFromClipboard"] = false;
	}

	if(!isset($arParams["usePspell"]))
	{
		$arParams["usePspell"] = COption::GetOptionString("fileman", "use_pspell", "N");
	}

	if(!isset($arParams["useCustomSpell"]))
	{
		$arParams["useCustomSpell"] = COption::GetOptionString("fileman", "use_custom_spell", "Y");
	}

	$arParams["showComponents"] = isset($arParams["showComponents"]) ? $arParams["showComponents"] : true;
	$arParams["showSnippets"] = isset($arParams["showSnippets"]) ? $arParams["showSnippets"] : true;
	$arParams["showSnippets"] = $arParams["showSnippets"] && $userSettings['show_snippets'] != 'N';

	$arParams["showTaskbars"] = $arParams["showTaskbars"] ?? null;
	if(!isset($arParams["initConponentParams"]))
		$arParams["initConponentParams"] = $arParams["showTaskbars"] !== false && $arParams["showComponents"] && ($arParams['limitPhpAccess'] || $arParams['bAllowPhp']);
	if (empty($arParams["actionUrl"]))
	{
		$arParams["actionUrl"] = $arParams["bbCode"] ? '/bitrix/tools/html_editor_action.php' : '/bitrix/admin/fileman_html_editor_action.php';
	}

	$arParams["lazyLoad"] = isset($arParams["lazyLoad"]) ? $arParams["lazyLoad"] : false;
	$arParams["copilotParams"] = is_array($arParams["copilotParams"] ?? null)
		? $arParams["copilotParams"]
		:
		[
			'moduleId' => 'main',
			'contextId' => 'bxhtmled_copilot',
			'category' => $this->GetAiCategory($this->id, $this->name),
		]
	;

	$isCopilotEnabled = $this->isCopilotEnabled();
	if (!$this->bAllowPhp && $isCopilotEnabled)
	{
		BitrixMainUIExtension::load(['ai.copilot']);
	}

	$this->jsConfig = array(
		'id' => $this->id,
		'isCopilotEnabled' => $isCopilotEnabled,
		'copilotParams' => $arParams["copilotParams"],
		'inputName' => $this->inputName,
		'content' => $this->content,
		'width' => $arParams['width'],
		'height' => $arParams['height'],
		'allowPhp' => $this->bAllowPhp,
		'limitPhpAccess' => $arParams['limitPhpAccess'],
		'templates' => $arTemplates,
		'templateId' => $templateId,
		'templateParams' => $templateParams,
		'componentFilter' => $arParams['componentFilter'] ?? null,
		'snippets' => $arSnippets,
		'placeholder' => isset($arParams['placeholder']) ? $arParams['placeholder'] : 'Text here...',
		'actionUrl' => $arParams["actionUrl"],
		'cssIframePath' => $this->cssIframePath,
		'bodyClass' => $arParams["bodyClass"],
		'fontSize' => isset($arParams['fontSize']) && is_string($arParams['fontSize']) ? $arParams['fontSize'] : '14px',
		'bodyId' => $arParams["bodyId"],
		'designTokens' => BitrixMainUIExtension::getHtml('ui.design-tokens'),
		'spellcheck_path' => $basePath.'html-spell.js?v='.filemtime($_SERVER['DOCUMENT_ROOT'].$basePath.'html-spell.js'),
		'usePspell' => $arParams["usePspell"],
		'useCustomSpell' => $arParams["useCustomSpell"],
		'bbCode' => $arParams["bbCode"],
		'askBeforeUnloadPage' => ($arParams["askBeforeUnloadPage"] ?? null) !== false,
		'settingsKey' => $settingsKey,
		'showComponents' => $arParams["showComponents"],
		'showSnippets' => $arParams["showSnippets"],
		// user settings
		'view' => $userSettings['view'],
		'splitVertical' => $userSettings['split_vertical'] ? true : false,
		'splitRatio' => $userSettings['split_ratio'],
		'taskbarShown' => $userSettings['taskbar_shown'] ? true : false,
		'taskbarWidth' => $userSettings['taskbar_width'],
		'lastSpecialchars' => $userSettings['specialchars'] ? explode('|', $userSettings['specialchars']) : false,
		'cleanEmptySpans' => $userSettings['clean_empty_spans'] != 'N',
		'pasteSetColors' => $userSettings['paste_clear_colors'] != 'N',
		'pasteSetBorders' => $userSettings['paste_clear_borders'] != 'N',
		'pasteSetDecor' => $userSettings['paste_clear_decor'] != 'N',
		'pasteClearTableDimen' => $userSettings['paste_clear_table_dimen'] != 'N',
		'linkDialogType' => $userSettings['link_dialog_type'],
		'lazyLoad' => $arParams["lazyLoad"],
		'siteId' => $siteId
	);

	if (($this->bAllowPhp || $arParams['limitPhpAccess']) && $arParams["showTaskbars"] !== false)
	{
		$this->jsConfig['components'] = self::GetComponents($templateId, false, $arParams['componentFilter'] ?? null);
	}

	if (isset($arParams["initAutosave"]))
		$this->jsConfig["initAutosave"] = $arParams["initAutosave"];

	if (isset($arParams["uploadImagesFromClipboard"]))
		$this->jsConfig["uploadImagesFromClipboard"] = $arParams["uploadImagesFromClipboard"];

	if (isset($arParams["useFileDialogs"]))
	{
		$this->jsConfig["useFileDialogs"] = $arParams["useFileDialogs"];
	}
	elseif (BitrixMainModuleManager::isModuleInstalled('bitrix24'))
	{
		$this->jsConfig["useFileDialogs"] = false;
	}

	if (isset($arParams["showTaskbars"]))
		$this->jsConfig["showTaskbars"] = $arParams["showTaskbars"];

	if (isset($arParams["showNodeNavi"]))
		$this->jsConfig["showNodeNavi"] = $arParams["showNodeNavi"];

	if (isset($arParams["controlsMap"]))
		$this->jsConfig["controlsMap"] = $arParams["controlsMap"];

	if (isset($arParams["arSmiles"]))
		$this->jsConfig["smiles"] = $arParams["arSmiles"];

	if (isset($arParams["arSmilesSet"]))
		$this->jsConfig["smileSets"] = $arParams["arSmilesSet"];

	if (isset($arParams["iframeCss"]))
		$this->jsConfig["iframeCss"] = $arParams["iframeCss"];


	if (isset($arParams["beforeUnloadMessage"]))
		$this->jsConfig["beforeUnloadMessage"] = $arParams["beforeUnloadMessage"];

	if (isset($arParams["setFocusAfterShow"]))
		$this->jsConfig["setFocusAfterShow"] = $arParams["setFocusAfterShow"];

	if (isset($arParams["relPath"]))
		$this->jsConfig["relPath"] = $arParams["relPath"];

	// autoresize
	if (isset($arParams["autoResize"]))
	{
		$this->jsConfig["autoResize"] = $arParams["autoResize"];
		if (isset($arParams['autoResizeOffset']))
			$this->jsConfig['autoResizeOffset'] = $arParams['autoResizeOffset'];
		if (isset($arParams['autoResizeMaxHeight']))
			$this->jsConfig['autoResizeMaxHeight'] = $arParams['autoResizeMaxHeight'];
		if (isset($arParams['autoResizeSaveSize']))
			$this->jsConfig['autoResizeSaveSize'] = $arParams['autoResizeSaveSize'] !== false;
	}

	if (isset($arParams["minBodyWidth"]))
		$this->jsConfig["minBodyWidth"] = $arParams["minBodyWidth"];
	if (isset($arParams["minBodyHeight"]))
		$this->jsConfig["minBodyHeight"] = $arParams["minBodyHeight"];
	if (isset($arParams["normalBodyWidth"]))
		$this->jsConfig["normalBodyWidth"] = $arParams["normalBodyWidth"];

	if (isset($arParams['autoLink']))
		$this->jsConfig['autoLink'] = $arParams['autoLink'];

	return $arParams;
}