From 71c0b1004a84207a1fcd4578a5630e5cfed15a74 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 11 May 2023 12:27:36 -0700 Subject: [PATCH] (NFC) ImportMap - Tweak line lengths --- CRM/Utils/Hook.php | 7 ++-- Civi/Core/ImportMap.php | 71 ++++++++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index a0b7ec25ab..30fb2b18cf 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -2857,9 +2857,10 @@ abstract class CRM_Utils_Hook { * may update the supported features. It is the subscribers' responsibility to conform * with browser standards. * @param array $context - * In the future, the `$context` may provide hints about the usage environment. Based on these - * hints, you may omit unnecessary mappings. However, in the absence of a clear hint, - * listeners should tend to over-communicate (i.e. report all the mappings that you can). + * In the future, the `$context` may provide hints about the usage environment. Based + * on these hints, you may omit unnecessary mappings. However, in the absence of a clear + * hint, listeners should tend to over-communicate (i.e. report all the mappings that + * you can). */ public static function esmImportMap(array &$importMap, array $context): void { $null = NULL; diff --git a/Civi/Core/ImportMap.php b/Civi/Core/ImportMap.php index bd699d5ee2..29e4138956 100644 --- a/Civi/Core/ImportMap.php +++ b/Civi/Core/ImportMap.php @@ -5,12 +5,14 @@ namespace Civi\Core; use Civi\Core\Event\GenericHookEvent; /** - * ECMAScript Modules (ESMs) allow you to load a JS file based on a physical-path or a logical-path. Compare: + * ECMAScript Modules (ESMs) allow you to load a JS file based on a physical-path or a + * logical-path. Compare: * * import { TableWidget } from 'https://example.com/sites/all/modules/civicrm/js/table-widget.js'; * import { TableWidget } from 'civicrm/js/tab-widget.js'; * - * The logical-path (`civicrm/js/tab-widget.js`) is much easier to read, and it adapts better to more environments. + * The logical-path (`civicrm/js/tab-widget.js`) is much easier to read, and it adapts + * better to more environments. * * Logical-paths must be defined with an import-map: * @@ -18,13 +20,14 @@ use Civi\Core\Event\GenericHookEvent; * { "import": {"civicrm/": "https://example.com/sites/all/modules/civicrm"}} * * - * This service defines the import-map for CiviCRM and its extensions. There are a few perspectives - * on how to use this service. + * This service defines the import-map for CiviCRM and its extensions. There are a few + * perspectives on how to use this service. * * ################################################################################### * ## Extension Developer: How to register additional mappings * - * If you are writing Javascript code for an extension, then you may want to define new mappings, e.g. + * If you are writing Javascript code for an extension, then you may want to define new + * mappings, e.g. * * function myext_civicrm_esmImportMap(array &$importMap, array $context): void { * $importMap['imports']['foo/'] = E::url('js/foo/'); @@ -34,16 +37,16 @@ use Civi\Core\Event\GenericHookEvent; * ################################################################################### * ## Core Developer: How to render a default SCRIPT tag * - * CiviCRM must generate the SCRIPT tag because (at time of writing) none of the supported UF's have - * a mechanism to do so. + * CiviCRM must generate the SCRIPT tag because (at time of writing) none of the supported + * UF's have a mechanism to do so. * - * - Logic: IF the current page has any ESM modules, THEN display a SCRIPT in the `html-header`. + * - Logic: IF the current page has any ESM modules, THEN display a SCRIPT in the HEAD. * - * - Implementation: The import-map listens to the `civi.region.render[html-header]` event. Whenever - * the header is generated, it makes a dynamic decision about whether to display. + * - Implementation: The import-map listens to the `civi.region.render[html-header]` event. + * Whenever the header is generated, it makes a dynamic decision about whether to display. * * ################################################################################### - * ## UF/CMS Developer: How to integrate Civi's import-map into the UF/CMS import-map. + * ## UF/CMS Developer: How to integrate import-maps from Civi and UF/CMS. * * In the future, UFs may define their own protocols for generating their own import-maps. * But the browser can only load one import-map. Therefore, the CiviCRM and UF import-maps @@ -68,10 +71,12 @@ class ImportMap extends \Civi\Core\Service\AutoService implements HookInterface /** * Do we need to send an import-map for the current page-view? * - * For the moment, we figure this dynamically -- based on whether any "esm" scripts have been added. During the - * early stages (where ESMs aren't in widespread use), this seems safer. However, in the future, we might find - * some kind of race (e.g. where the system renders "" before it decides on a specific "", json_encode($importMap, $flags)); + + $flags = JSON_UNESCAPED_SLASHES; + if (\Civi::settings()->get('debug_enabled')) { + $flags |= JSON_PRETTY_PRINT; } + return sprintf("", json_encode($importMap, $flags)); } } -- 2.25.1