Skip to content

Commit 4388b4e

Browse files
authored
Merge pull request #231 from wpengine/enhancement-better-url-param-handle
fix: URL param handle and move inline JS to a separate file
2 parents 6893c93 + 8891551 commit 4388b4e

3 files changed

Lines changed: 53 additions & 24 deletions

File tree

plugins/hwp-previews/assets/css/hwp-previews.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
.hwp-previews-tag-cloud {
2+
display: flex;
3+
flex-wrap: wrap;
4+
gap: 8px;
5+
}
6+
17
.form-table td input.hwp-previews-url {
28
width: calc(99% - 24px);
39
display: inline-block;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
document.addEventListener("DOMContentLoaded", () => {
2+
const buttons = document.querySelectorAll(".hwp-previews-insert-tag");
3+
const input = document.querySelector(".hwp-previews-url");
4+
5+
function setTouched() {
6+
this.setAttribute("data-touched", "true");
7+
this.removeEventListener("focus", setTouched);
8+
}
9+
10+
function insertTag() {
11+
const tag = this.textContent.trim();
12+
const isTouched = input.dataset.touched;
13+
14+
if (!input) return;
15+
16+
// Get cursor position
17+
let cursorPos = input.selectionEnd;
18+
19+
// If cursor position is at the start, set it to the end of the input value
20+
if (cursorPos === 0 && !isTouched) cursorPos = input.value.length;
21+
22+
// Split text at cursor position
23+
const textBefore = input.value.substring(0, cursorPos);
24+
const textAfter = input.value.substring(cursorPos);
25+
26+
// Insert link text at cursor position
27+
input.value = textBefore + tag + textAfter;
28+
29+
// Set cursor position after inserted text
30+
const newCursorPos = cursorPos + tag.length;
31+
input.setSelectionRange(newCursorPos, newCursorPos);
32+
33+
// Focus the input
34+
input.focus();
35+
}
36+
37+
// Mark the input as touched to prevent inserting at the start
38+
input.addEventListener("focus", setTouched);
39+
40+
for (const button of buttons) {
41+
button.addEventListener("click", insertTag);
42+
}
43+
});

plugins/hwp-previews/src/Admin/Settings/Templates/settings-page-main.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,12 @@
4545
<div class="postbox">
4646
<h2><?php esc_html_e( 'Available Preview URL Parameters', 'hwp-previews' ); ?></h2>
4747
<div class="inside">
48-
<div class="tag-cloud">
48+
<div class="hwp-previews-tag-cloud">
4949
<?php foreach ( $hwp_previews_params as $hwp_previews_param_name => $hwp_previews_param_desc ) : ?>
50-
<a href="#" class="button button-secondary"
51-
title="<?php echo esc_attr( $hwp_previews_param_desc ); ?>"
52-
style="margin:0 5px 10px 0; font-size: 16px" onclick="
53-
const input = document.querySelector('.hwp-previews-url');
54-
const linkText = this.textContent.trim();
55-
56-
// Get cursor position
57-
const cursorPos = input.selectionStart;
58-
59-
// Split text at cursor position
60-
const textBefore = input.value.substring(0, cursorPos);
61-
const textAfter = input.value.substring(cursorPos);
62-
63-
// Insert link text at cursor position
64-
input.value = textBefore + linkText + textAfter;
65-
66-
// Set cursor position after inserted text
67-
const newCursorPos = cursorPos + linkText.length;
68-
input.setSelectionRange(newCursorPos, newCursorPos);
69-
70-
// Focus the input
71-
input.focus();">
50+
<button type="button" class="button button-secondary hwp-previews-insert-tag"
51+
title="<?php echo esc_attr( $hwp_previews_param_desc ); ?>">
7252
<?php echo esc_html( "{{$hwp_previews_param_name}}" ); ?>
73-
</a>
53+
</button>
7454
<?php endforeach; ?>
7555
</div>
7656
</div>

0 commit comments

Comments
 (0)