Переглянути джерело

refactor: deduplicate OS-specific hotkey detection into a shared helper

Ribas160 2 місяців тому
батько
коміт
ec2d230d1b
5 змінених файлів з 31 додано та 6 видалено
  1. 13 0
      lib/I18n.php
  2. 1 2
      tpl/bootstrap.php
  3. 1 2
      tpl/bootstrap5.php
  4. 1 2
      tpl/shortenerproxy.php
  5. 15 0
      tst/I18nTest.php

+ 13 - 0
lib/I18n.php

@@ -297,6 +297,19 @@ class I18n
         return in_array(self::$_language, array('ar', 'he'));
     }
 
+    /**
+     * get OS-specific copy hotkey modifier key name based on user agent
+     *
+     * @access public
+     * @static
+     * @return string 'Cmd' on macOS, 'Ctrl' otherwise
+     */
+    public static function getCopyHotkey()
+    {
+        return isset($_SERVER['HTTP_USER_AGENT']) &&
+            strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false ? 'Cmd' : 'Ctrl';
+    }
+
     /**
      * set the default language
      *

+ 1 - 2
tpl/bootstrap.php

@@ -626,8 +626,7 @@ endif;
 						<div class="media-body media-middle">
 							<small id="copyShortcutHintText" class="hidden-xs">
 								<?php
-									$hotkey = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false ? 'Cmd' : 'Ctrl';
-									echo I18n::_("To copy document press on the copy button or use the clipboard shortcut <kbd>%s</kbd>+<kbd>c</kbd>", $hotkey)
+									echo I18n::_("To copy document press on the copy button or use the clipboard shortcut <kbd>%s</kbd>+<kbd>c</kbd>", I18n::getCopyHotkey())
 								?>
 							</small>
 						</div>

+ 1 - 2
tpl/bootstrap5.php

@@ -483,8 +483,7 @@ endif;
 					<h6 id="copyShortcutHint" class="col-md-12 nav justify-content-between align-items-center mb-2 hidden">
 						<small id="copyShortcutHintText" class="d-none d-md-inline">
 							<?php
-								$hotkey = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false ? 'Cmd' : 'Ctrl';
-								echo I18n::_("To copy document press on the copy button or use the clipboard shortcut <kbd>%s</kbd>+<kbd>c</kbd>", $hotkey)
+								echo I18n::_("To copy document press on the copy button or use the clipboard shortcut <kbd>%s</kbd>+<kbd>c</kbd>", I18n::getCopyHotkey())
 							?>
 						</small>
 						<button type="button" id="copyShortcutHintBtn" class="btn btn-secondary ms-auto"><?php echo I18n::_('Copy'); ?></button>

+ 1 - 2
tpl/shortenerproxy.php

@@ -14,8 +14,7 @@ if (empty($ERROR)) :
 ?>
 		<p>
 			<?php
-				$hotkey = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false ? 'Cmd' : 'Ctrl';
-				echo I18n::_('Your document is <a id="pasteurl" href="%s">%s</a> <span id="copyhint">(Hit <kbd>%s</kbd>+<kbd>c</kbd> to copy)</span>', $SHORTURL, $SHORTURL, $hotkey);
+				echo I18n::_('Your document is <a id="pasteurl" href="%s">%s</a> <span id="copyhint">(Hit <kbd>%s</kbd>+<kbd>c</kbd> to copy)</span>', $SHORTURL, $SHORTURL, I18n::getCopyHotkey());
 			?>
 		</p>
 <?php

+ 15 - 0
tst/I18nTest.php

@@ -231,6 +231,21 @@ class I18nTest extends TestCase
         Helper::rmDir($path);
     }
 
+    public function testGetCopyHotkey()
+    {
+        $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)';
+        $this->assertEquals('Cmd', I18n::getCopyHotkey(), 'returns Cmd on macOS');
+
+        $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)';
+        $this->assertEquals('Ctrl', I18n::getCopyHotkey(), 'returns Ctrl on Windows');
+
+        $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; Linux x86_64)';
+        $this->assertEquals('Ctrl', I18n::getCopyHotkey(), 'returns Ctrl on Linux');
+
+        unset($_SERVER['HTTP_USER_AGENT']);
+        $this->assertEquals('Ctrl', I18n::getCopyHotkey(), 'returns Ctrl when user agent absent');
+    }
+
     public function testMessageIdsExistInAllLanguages()
     {
         $messageIds = array();