Skip to content

Commit

Permalink
feat: add CopyFieldNames tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Nov 2, 2023
1 parent fe51ea8 commit 822b36b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Tweak.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RockMigrations\Tweaks;

use ProcessWire\RockMigrations;
use ProcessWire\WireData;

abstract class Tweak extends WireData
Expand All @@ -16,6 +17,11 @@ public function ready()
{
}

public function rockmigrations(): RockMigrations
{
return $this->wire->modules->get('RockMigrations');
}

public function __debugInfo()
{
return [
Expand Down
26 changes: 26 additions & 0 deletions tweaks/CopyFieldNames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
$(document).ready(function () {
// Copy a string to the clipboard
function copyToClipboard(string) {
const $temp = $('<input type="text" value="' + string + '">');
$("body").append($temp);
$temp.select();
document.execCommand("copy");
$temp.remove();
}

// When InputfieldHeader is clicked
$(document).on("click", ".InputfieldHeader", function (event) {
let text = "";
if (!event.shiftKey) return;

event.preventDefault();
event.stopImmediatePropagation();
text = $(this).attr("for");
if (!text) text = $(this).parent().attr("id");
text = text.replace(/^Inputfield_|wrap_Inputfield_|wrap_/, "").trim();
if (!text) return;

copyToClipboard(text);
$(this).effect("highlight", {}, 500);
});
});
24 changes: 24 additions & 0 deletions tweaks/CopyFieldNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace RockMigrations\Tweaks;

use ProcessWire\HookEvent;

class CopyFieldNames extends Tweak
{
public $description = "Copy field names on shift-click by SuperUsers "
. "<a href=https://processwire.com/talk/topic/29071-using-javascript-to-copy-page-ids-from-page-list-and-field-nameslabels-from-inputfields/ target=_blank><i class='fa fa-info-circle'></i></a>";

public function init()
{
// Add custom JS file to $config->scripts FilenameArray
// This adds the custom JS fairly early in the FilenameArray which allows for stopping
// event propagation so clicks on InputfieldHeader do not also expand/collapse InputfieldContent
$this->wire->addHookBefore('ProcessController::execute', function (HookEvent $event) {
if (!$event->wire->user->isSuperuser()) return;
$rm = $this->rockmigrations();
$url = $rm->toUrl(__DIR__ . "/CopyFieldNames.js", true);
$this->wire->config->scripts->add($url);
});
}
}

0 comments on commit 822b36b

Please sign in to comment.