Skip to content

Commit f070679

Browse files
committed
main.addon.DocumentHead: update() => remote method #7765
1 parent 77421db commit f070679

2 files changed

Lines changed: 38 additions & 21 deletions

File tree

apps/portal/view/ViewportController.mjs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,9 @@ class ViewportController extends Controller {
426426
let {windowId} = this,
427427
DocumentHead = await Neo.currentWorker.getAddon('DocumentHead', windowId);
428428

429-
DocumentHead.setTitle({value: title, windowId});
430-
431-
DocumentHead.setTag({
432-
content: description,
433-
name : 'description',
434-
tag : 'meta',
429+
await DocumentHead.update({
430+
tag: {tag: 'meta', name: 'description', content: description},
431+
title,
435432
windowId
436433
})
437434
}

src/main/addon/DocumentHead.mjs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class DocumentHead extends Base {
3333
'setCanonical',
3434
'setLdJson',
3535
'setTag',
36-
'setTitle'
36+
'setTitle',
37+
'update'
3738
]
3839
}
3940
}
@@ -43,7 +44,7 @@ class DocumentHead extends Base {
4344
* @returns {String|null} The canonical URL, or null if not found.
4445
*/
4546
getCanonical() {
46-
const canonical = document.head.querySelector('link[rel="canonical"]');
47+
let canonical = document.head.querySelector('link[rel="canonical"]');
4748
return canonical?.href || null
4849
}
4950

@@ -52,7 +53,7 @@ class DocumentHead extends Base {
5253
* @returns {Object|null} The parsed JSON data, or null if the tag is not found or parsing fails.
5354
*/
5455
getLdJson() {
55-
const script = document.head.querySelector('script[type="application/ld+json"]');
56+
let script = document.head.querySelector('script[type="application/ld+json"]');
5657

5758
if (script) {
5859
try {
@@ -72,8 +73,8 @@ class DocumentHead extends Base {
7273
* @returns {Object|null} An object containing all attributes of the found tag, or null if not found.
7374
*/
7475
getTag(config) {
75-
const {tag, ...attributes} = config;
76-
let selector;
76+
let {tag, ...attributes} = config,
77+
selector;
7778

7879
if (tag === 'meta') {
7980
if (attributes.name) {
@@ -126,9 +127,10 @@ class DocumentHead extends Base {
126127

127128
/**
128129
* Creates or updates the ld+json script tag with new structured data.
129-
* @param {Object} data The JavaScript object to be stringified and set as the script's content.
130+
* @param {Object} data
131+
* @param {Object} data.value The JavaScript object to be stringified and set as the script's content.
130132
*/
131-
setLdJson({data}) {
133+
setLdJson({value}) {
132134
let script = document.head.querySelector('script[type="application/ld+json"]');
133135

134136
if (!script) {
@@ -137,19 +139,20 @@ class DocumentHead extends Base {
137139
document.head.appendChild(script)
138140
}
139141

140-
script.textContent = JSON.stringify(data, null, 2)
142+
script.textContent = JSON.stringify(value, null, 2)
141143
}
142144

143145
/**
144146
* Creates or updates a <meta> or <link> tag in the document head.
145147
* It finds an existing tag based on the same 'name', 'property' (for meta), or 'rel' (for link),
146148
* updates its attributes, or creates a new tag if one does not exist. This method is designed
147149
* to be efficient by modifying existing tags in place rather than removing and re-adding them.
148-
* @param {Object} config The configuration for the tag, including the tag name and its attributes.
150+
* @param {Object} data
151+
* @param {Object} data.value The configuration for the tag, including the tag name and its attributes.
149152
*/
150-
setTag(config) {
151-
const {tag, ...attributes} = config;
152-
let selector, tagElement;
153+
setTag({value}) {
154+
let {tag, ...attributes} = value,
155+
selector, tagElement;
153156

154157
if (tag === 'meta') {
155158
if (attributes.name) {
@@ -166,11 +169,11 @@ class DocumentHead extends Base {
166169
tagElement = selector ? document.head.querySelector(selector) : null;
167170

168171
if (!tagElement) {
169-
tagElement = document.createElement(tag);
172+
tagElement = document.createElement(tag)
170173
}
171174

172-
for (const [key, value] of Object.entries(attributes)) {
173-
tagElement.setAttribute(key, value)
175+
for (const [key, val] of Object.entries(attributes)) {
176+
tagElement.setAttribute(key, val)
174177
}
175178

176179
// Only append if it's a new element
@@ -187,6 +190,23 @@ class DocumentHead extends Base {
187190
setTitle({value}) {
188191
document.title = value
189192
}
193+
194+
/**
195+
* Convenience shortcut to change multiple items inside one remote method access call.
196+
* @param {Object} data
197+
* @param {String} [data.canonicalUrl] The canonical URL to set.
198+
* @param {Object} [data.ldJson] The JavaScript object to be stringified and set as the script's content.
199+
* @param {Object} [data.tag] The configuration for the tag, including the tag name and its attributes.
200+
* @param {String} [data.title] The new title for the document.
201+
*/
202+
update({canonicalUrl, ldJson, tag, title}) {
203+
let me = this;
204+
205+
canonicalUrl && me.setCanonical({url: canonicalUrl});
206+
ldJson && me.setLdJson({value: ldJson});
207+
tag && me.setTag({value: tag});
208+
title && me.setTitle({value: title})
209+
}
190210
}
191211

192212
export default Neo.setupClass(DocumentHead);

0 commit comments

Comments
 (0)