From e113d30490eeac2a148271d820735f75f83cdf24 Mon Sep 17 00:00:00 2001 From: beu Date: Fri, 25 Jul 2025 15:32:40 +0200 Subject: [PATCH] add clipboard feature --- libs/FML/Controls/Control.php | 16 +++ libs/FML/Script/Features/Clipboard.php | 134 +++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 libs/FML/Script/Features/Clipboard.php diff --git a/libs/FML/Controls/Control.php b/libs/FML/Controls/Control.php index 070e2fd9..f369c53d 100644 --- a/libs/FML/Controls/Control.php +++ b/libs/FML/Controls/Control.php @@ -3,6 +3,7 @@ namespace FML\Controls; use FML\Script\Features\ActionTrigger; +use FML\Script\Features\Clipboard; use FML\Script\Features\ControlScript; use FML\Script\Features\MapInfo; use FML\Script\Features\PlayerProfile; @@ -919,6 +920,21 @@ abstract class Control implements Identifiable, Renderable, ScriptFeatureable return $this; } + /** + * Add a custom Control Script text part + * + * @api + * @param string $scriptText Script text + * @param string $label (optional) Script label name + * @return static + */ + public function addClipboardFeature(mixed $value, ?Label $tooltipLabel = null) + { + $clipboard = new Clipboard($this, $value, $tooltipLabel); + $this->addScriptFeature($clipboard); + return $this; + } + /** * Add a custom Control Script text part * diff --git a/libs/FML/Script/Features/Clipboard.php b/libs/FML/Script/Features/Clipboard.php new file mode 100644 index 00000000..6ea94b04 --- /dev/null +++ b/libs/FML/Script/Features/Clipboard.php @@ -0,0 +1,134 @@ + + * @copyright FancyManiaLinks Copyright © 2017 Steffen Schröder + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + */ +class Clipboard extends ScriptFeature +{ + public const DATASET_PROPERTY = 'clipboard-data'; + + + /** + * @var Control $control + */ + protected $control = null; + + /** + * @var string $value value + */ + protected $value = null; + + /** + * @var Control $tooltipControl Tooltip Control + */ + protected $tooltipControl = null; + + + /** + * Construct a new Tooltip + * + * @api + * @param Control $hoverControl Control + * @param string $value Value to set in the Clipboard + * @param Control $tooltipControl (optional) If tooltip is used + */ + public function __construct(Control $control, mixed $value, ?Control $tooltipControl = null) + { + $this->setControl($control); + $this->setValue($value); + + if ($tooltipControl) { + $this->setTooltipControl($tooltipControl); + } + } + + /** + * Set the Control + * + * @api + * @param Control $control Control + * @return static + */ + public function setControl(Control $control) + { + $control->checkId(); + + if ($this->control !== null) { + $this->control->removeDataAttribute(self::DATASET_PROPERTY); + } + + $this->control = $control; + + if ($this->value !== null) { + $this->setValue($this->value); + } + + return $this; + } + + /** + * Set the value to copy + * + * @api + * @param mixed $value value + * @return static + */ + public function setValue(mixed $value) + { + $this->value = (string) $value; + $this->control->addDataAttribute(self::DATASET_PROPERTY, $this->value); + + return $this; + } + + /** + * Set the Tooltip Control + * + * @api + * @param Control $tooltipControl Tooltip Control + * @return static + */ + public function setTooltipControl(Control $tooltipControl) + { + $tooltipControl->checkId(); + $this->tooltipControl = $tooltipControl; + $tooltip = new Tooltip($this->control, $tooltipControl, false, false, "Click to copy"); + $this->control->addScriptFeature($tooltip); + + return $this; + } + + /** + * @see ScriptFeature::prepare() + */ + public function prepare(Script $script) + { + $controlId = Builder::escapeText($this->control->getId()); + $datasetProperty = Builder::escapeText(self::DATASET_PROPERTY); + + $scriptText = " +if (Event.Control.ControlId == {$controlId}) { + log(\"clipboard \"^ Event.Control.DataAttributeExists({$datasetProperty})); + if (System != Null && Event.Control.DataAttributeExists({$datasetProperty})) { + System.ClipboardSet(Event.Control.DataAttributeGet({$datasetProperty})); + } +}"; + $script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $scriptText); + + return $this; + } + +}