Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of DOMNodeInserted in our code #1011

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,35 @@ const metasmoke = window.metasmoke = {
}
},

showRenderedTab: () => {
const renderMode = metasmoke.storage['post-render-mode'];
if (renderMode) {
$(`.post-render-mode[data-render-mode="${renderMode}"]`).tab('show');
}
},

setPostRenderModes: () => {
$(document.body).on('click', '.post-render-mode', ev => {
const mode = $(ev.target).data('render-mode');
metasmoke.storage['post-render-mode'] = mode;
metasmoke.debug(`default render mode updated to ${mode}`);
});

if (metasmoke.storage['post-render-mode']) {
$(`.post-render-mode[data-render-mode="${metasmoke.storage['post-render-mode']}"]`).tab('show');
}

$(document.body).on('DOMNodeInserted', '.post-body, .review-item-container', () => {
$(`.post-render-mode[data-render-mode="${metasmoke.storage['post-render-mode']}"]`).tab('show');
});
metasmoke.init.showRenderedTab();
/* The following set of operations replaces a DOMNodeInserted listener.
* It won't catch every possible new code addition of a .post-render-mode node, but it will catch all currently
* existing cases where we add such a node into the DOM, and is much more computationally effecient than
* using a MutationObserver looking at all node insertions in the entire body.
* The drawback is that it may be necessary to add special cases, such as is used for displaying reviews and
* the custom MS-review-loaded event.
* Note that this code is similar to the triggers used for rendering the preview. Thus, if a change is needed
* here, it's likely needed there too.
*/
onLoad(metasmoke.init.showRenderedTab);
// Delaying 11 ms is one ms after the 10 ms delay for rendering the preview.
$(document).ajaxComplete(() => setTimeout(metasmoke.init.showRenderedTab, 11));
// This is delayed in order to be done after the preview is rendered.
$(window).on('MS-review-loaded', () => setTimeout(metasmoke.init.showRenderedTab, 1));
},

setDocumentTitle: () => {
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/post_preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import { onLoad, route, addLocalSettingsPanel } from './util';
});
}

// The following is similar to what's used to select the post tab in application.js. If a change is needed here,
// then one is very likely to be needed there too.
onLoad(addPostPreviews);
$(document).ajaxComplete(() => setTimeout(addPostPreviews, 10));
$(window).on('MS-review-loaded', addPostPreviews);
Expand Down
Loading