Skip to content

Commit

Permalink
chore: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
YUCLing committed Nov 18, 2024
1 parent ef8a333 commit 1644f2e
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default abstract class AutocompleteDropdown<
.onSelect(this.selectSuggestion.bind(this), true)
.bindTo(input);

input.addEventListener('focus', function() {
input.addEventListener('focus', function () {
component.hasFocus = true;
m.redraw();

Expand Down Expand Up @@ -174,7 +174,7 @@ export default abstract class AutocompleteDropdown<
fixedIndex = 0;
}

items.forEach(el => el.classList.remove('active'));
items.forEach((el) => el.classList.remove('active'));
const item = items[fixedIndex];
const dropdown = item.parentElement!;
item.classList.add('active');
Expand All @@ -201,7 +201,7 @@ export default abstract class AutocompleteDropdown<
if (typeof scrollTop !== 'undefined') {
dropdown.scrollTo({
top: scrollTop,
behavior: 'smooth'
behavior: 'smooth',
});
}
}
Expand Down
8 changes: 6 additions & 2 deletions framework/core/js/src/common/components/FormModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default abstract class FormModal<ModalAttrs extends IFormModalAttrs = IFo
* @remark Focuses the first input in the modal.
*/
onready(): void {
const input = this.element.querySelector('input, select, textarea') as (HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement);
const input = this.element.querySelector('input, select, textarea') as HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
if (!input) return;
input.focus();
if ('select' in input) input.select();
Expand All @@ -46,7 +46,11 @@ export default abstract class FormModal<ModalAttrs extends IFormModalAttrs = IFo
m.redraw();

if (error.status === 422 && error.response?.errors) {
(this.element.querySelector(`form [name='${(error.response.errors as any[])[0].source.pointer.replace('/data/attributes/', '')}']`) as HTMLInputElement)?.select();
(
this.element.querySelector(
`form [name='${(error.response.errors as any[])[0].source.pointer.replace('/data/attributes/', '')}']`
) as HTMLInputElement
)?.select();
} else {
this.onready();
}
Expand Down
8 changes: 5 additions & 3 deletions framework/core/js/src/common/components/SearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,11 @@ export default class SearchModal<CustomAttrs extends ISearchModalAttrs = ISearch
.bindTo(input);

// Handle input key events on the search input, triggering results to load.
['input', 'focus'].forEach((ev) => input.addEventListener(ev as 'input'| 'focus', function() {
search(this.value.toLowerCase());
}));
['input', 'focus'].forEach((ev) =>
input.addEventListener(ev as 'input' | 'focus', function () {
search(this.value.toLowerCase());
})
);
}

onremove(vnode: Mithril.VnodeDOM<CustomAttrs, this>) {
Expand Down
2 changes: 1 addition & 1 deletion framework/core/js/src/common/components/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export default class Tooltip extends Component<TooltipAttrs> {
html,
delay: delay || 0,
placement: position,
trigger: trigger as any
trigger: trigger as any,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class UploadImageButton<CustomAttrs extends IUploadImageButtonAtt
body,
})
.then(this.success.bind(this), this.failure.bind(this));
})
});
}

remove() {
Expand Down
5 changes: 1 addition & 4 deletions framework/core/js/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import popperMobileModifier from './utils/popperMobileModifier';

Dropdown.Default.popperConfig = {
strategy: 'fixed',
modifiers: [
popperMobileModifier
]
modifiers: [popperMobileModifier],
};

dayjs.extend(relativeTime);
Expand All @@ -30,4 +28,3 @@ import app from './app';
export { app };

import './utils/arrayFlatPolyfill';

6 changes: 3 additions & 3 deletions framework/core/js/src/common/utils/heightWithMargin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function heightWithMargin(element: HTMLElement): number {
const style = getComputedStyle(element);
return element.getBoundingClientRect().height + parseInt(style.marginBottom, 10) + parseInt(style.marginTop, 10);
}
const style = getComputedStyle(element);
return element.getBoundingClientRect().height + parseInt(style.marginBottom, 10) + parseInt(style.marginTop, 10);
}
8 changes: 4 additions & 4 deletions framework/core/js/src/common/utils/popperMobileModifier.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Modifier } from "@popperjs/core";
import { type Modifier } from '@popperjs/core';

export default {
name: 'responsiveMobile',
Expand All @@ -13,8 +13,8 @@ export default {
left: null as unknown as string,
top: null as unknown as string,
bottom: null as unknown as string,
transform: null as unknown as string
transform: null as unknown as string,
};
}
}
} satisfies Modifier<any, any>;
},
} satisfies Modifier<any, any>;
26 changes: 13 additions & 13 deletions framework/core/js/src/common/utils/scrollEnd.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
/**
* A utility function that waits for the scroll to end.
* Due to lack of support from some browsers, this is a workaround for `scrollend` event.
*
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTo#behavior
* @see https://caniuse.com/mdn-api_element_scrollend_event
* @param container The container element to wait scroll end on.
* @returns A promise that resolves when the scroll is ended.
*/
export default function scrollEnd(container: HTMLElement): Promise<void> {
let lastTop = container.scrollTop;
let lastTop = container.scrollTop;

return new Promise((resolve) => {
const animFrame = () => {
if (lastTop === container.scrollTop) {
resolve();
} else {
requestAnimationFrame(animFrame);
lastTop = container.scrollTop;
}
};
return new Promise((resolve) => {
const animFrame = () => {
if (lastTop === container.scrollTop) {
resolve();
} else {
requestAnimationFrame(animFrame);
});
}
lastTop = container.scrollTop;
}
};
requestAnimationFrame(animFrame);
});
}
7 changes: 3 additions & 4 deletions framework/core/js/src/forum/components/AbstractPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ export default abstract class AbstractPost<CustomAttrs extends IAbstractPostAttr
onupdate(vnode: Mithril.VnodeDOM<CustomAttrs, this>) {
super.onupdate(vnode);

this.element.querySelector('.Post-actions')?.classList.toggle(
'openWithin',
this.element.querySelector('.Post-controls')?.classList.contains('open')
);
this.element
.querySelector('.Post-actions')
?.classList.toggle('openWithin', this.element.querySelector('.Post-controls')?.classList.contains('open'));
}

elementAttrs(): Record<string, unknown> {
Expand Down
1 change: 1 addition & 0 deletions framework/core/js/src/forum/components/AffixedSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class AffixedSidebar extends Component {
}

onresize() {
return;
const $sidebar = this.$();
const $header = $('#header');
const $footer = $('#footer');
Expand Down
14 changes: 9 additions & 5 deletions framework/core/js/src/forum/components/CommentPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default class CommentPost extends Post {
script.textContent = el.textContent;
Array.from(el.attributes).forEach((attr) => script.setAttribute(attr.name, attr.value));
this.parentNode.replaceChild(script, el);
})
});
}

this.contentHtml = contentHtml;
Expand Down Expand Up @@ -193,9 +193,13 @@ export default class CommentPost extends Post {
const userCard = this.element.querySelector('.UserCard');
if (!userCard) return;
userCard.classList.remove('show');
userCard.addEventListener('transitionend', () => {
this.cardVisible = false;
m.redraw();
}, { once: true });
userCard.addEventListener(
'transitionend',
() => {
this.cardVisible = false;
m.redraw();
},
{ once: true }
);
}
}
3 changes: 1 addition & 2 deletions framework/core/js/src/forum/components/DiscussionListPane.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ export default class DiscussionListPane extends Component {
}

onremove(vnode) {
if (vnode.dom)
app.cache.discussionListPaneScrollTop = vnode.dom.scrollTop;
if (vnode.dom) app.cache.discussionListPaneScrollTop = vnode.dom.scrollTop;
document.removeEventListener('mousemove', hotEdge);
}

Expand Down
2 changes: 1 addition & 1 deletion framework/core/js/src/forum/components/HeaderList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class HeaderList<CustomAttrs extends IHeaderListAttrs = IHeaderLi
super.oncreate(vnode);

if (this.attrs.loadMore) {
this.content = this.element.querySelector(".HeaderList-content");
this.content = this.element.querySelector('.HeaderList-content');

// If we are on the notifications page, the window will be scrolling and not the $notifications element.
this.scrollParent = this.inPanel() ? this.content : document.documentElement;
Expand Down
6 changes: 3 additions & 3 deletions framework/core/js/src/forum/components/IndexPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ export default class IndexPage<CustomAttrs extends IIndexPageAttrs = IIndexPageA
// Otherwise, we've just changed the filter, so we should go to the top of the page.
if (this.lastDiscussion) {
window.scrollTo({
top: scrollTop - oldHeroHeight + heroHeight
top: scrollTop - oldHeroHeight + heroHeight,
});
} else {
window.scrollTo({
top: 0
top: 0,
});
}

Expand All @@ -122,7 +122,7 @@ export default class IndexPage<CustomAttrs extends IIndexPageAttrs = IIndexPageA

if (discussionTop < scrollTop + indexTop || discussionBottom > scrollTop + indexBottom) {
window.scrollTo({
top: discussionTop - indexTop
top: discussionTop - indexTop,
});
}
}
Expand Down
20 changes: 13 additions & 7 deletions framework/core/js/src/forum/components/NotificationGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,22 @@ export default class NotificationGrid extends Component {
super.oncreate(vnode);

this.element.querySelectorAll('thead .NotificationGrid-groupToggle').forEach((toggle) => {
['mouseenter', 'mouseleave'].forEach((ev) => toggle.addEventListener(ev, function(e) {
const i = parseInt(Array.from(this.parentElement.children).indexOf(this), 10) + 1;
this.closest('table').querySelectorAll('td:nth-child(' + i + ')').forEach((td) => td.classList.toggle('highlighted', e.type === 'mouseenter'));
}));
['mouseenter', 'mouseleave'].forEach((ev) =>
toggle.addEventListener(ev, function (e) {
const i = parseInt(Array.from(this.parentElement.children).indexOf(this), 10) + 1;
this.closest('table')
.querySelectorAll('td:nth-child(' + i + ')')
.forEach((td) => td.classList.toggle('highlighted', e.type === 'mouseenter'));
})
);
});

this.element.querySelectorAll('tbody .NotificationGrid-groupToggle').forEach((toggle) => {
['mouseenter', 'mouseleave'].forEach((ev) => toggle.addEventListener(ev, function(e) {
this.parentElement.querySelectorAll('td').forEach((td) => td.classList.toggle('highlighted', e.type === 'mouseenter'));
}));
['mouseenter', 'mouseleave'].forEach((ev) =>
toggle.addEventListener(ev, function (e) {
this.parentElement.querySelectorAll('td').forEach((td) => td.classList.toggle('highlighted', e.type === 'mouseenter'));
})
);
});
}

Expand Down
37 changes: 23 additions & 14 deletions framework/core/js/src/forum/components/PostStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ export default class PostStream extends Component {
* @return {Promise}
*/
scrollToIndex(index, animate, reply) {
const item = reply ? document.querySelector('.PostStream-item:last-child') : this.element.querySelector(`.PostStream-item[data-index='${index}']`);
const item = reply
? document.querySelector('.PostStream-item:last-child')
: this.element.querySelector(`.PostStream-item[data-index='${index}']`);

this.scrollToItem(item, animate, true, reply);

Expand All @@ -371,18 +373,18 @@ export default class PostStream extends Component {
const index = parseInt(item.getAttribute('data-index'));

const itemTopOffset = item.getBoundingClientRect().top + document.documentElement.scrollTop;
const itemTop = itemTopOffset- this.getMarginTop();
const itemBottom = itemTopOffset + item.clientHeight;
const scrollTop = document.documentElement.scrollTop;
const scrollBottom = scrollTop + window.innerHeight;

// If the item is already in the viewport, we may not need to scroll.
// If we're scrolling to the reply placeholder, we'll make sure its
// bottom will line up with the top of the composer.
if (force || itemTop < scrollTop || itemBottom > scrollBottom) {
const top = reply ? itemBottom - window.innerHeight + app.composer.computedHeight() : item.parentElement.children[0] == item ? 0 : itemTop;

document.documentElement.scrollTo({ top, behavior: animate ? 'smooth' : 'instant' });
const itemTop = itemTopOffset - this.getMarginTop();
const itemBottom = itemTopOffset + item.clientHeight;
const scrollTop = document.documentElement.scrollTop;
const scrollBottom = scrollTop + window.innerHeight;

// If the item is already in the viewport, we may not need to scroll.
// If we're scrolling to the reply placeholder, we'll make sure its
// bottom will line up with the top of the composer.
if (force || itemTop < scrollTop || itemBottom > scrollBottom) {
const top = reply ? itemBottom - window.innerHeight + app.composer.computedHeight() : item.parentElement.children[0] == item ? 0 : itemTop;

document.documentElement.scrollTo({ top, behavior: animate ? 'smooth' : 'instant' });
}

const updateScrubberHeight = () => {
Expand Down Expand Up @@ -411,7 +413,14 @@ export default class PostStream extends Component {
let itemRect;
if (reply) {
const placeholder = document.querySelector('.PostStream-item:last-child');
window.scrollTo({ top: placeholder.getBoundingClientRect().top + document.documentElement.scrollTop + placeholder.clientHeight - window.innerHeight + app.composer.computedHeight() });
window.scrollTo({
top:
placeholder.getBoundingClientRect().top +
document.documentElement.scrollTop +
placeholder.clientHeight -
window.innerHeight +
app.composer.computedHeight(),
});
} else if (index === 0) {
window.scrollTo({ top: 0 });
} else if ((itemRect = document.querySelector(`.PostStream-item[data-index='${index}']`))) {
Expand Down
12 changes: 6 additions & 6 deletions framework/core/js/src/forum/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export default class Search<T extends SearchAttrs = SearchAttrs> extends Compone
searchResults.addEventListener('mousedown', (e) => e.preventDefault());
searchResults.addEventListener('click', () => this.element.querySelector('input')?.blur());
// Whenever the mouse is hovered over a search result, highlight it.
searchResults.addEventListener('mouseenter', function(e) {
searchResults.addEventListener('mouseenter', function (e) {
const el = e.target as HTMLElement;
if (el.parentElement != searchResults || el.tagName != 'LI' || el.classList.contains('Dropdown-header')) return;
search.setIndex(search.selectableItems().indexOf(el as HTMLLIElement));
Expand All @@ -219,8 +219,8 @@ export default class Search<T extends SearchAttrs = SearchAttrs> extends Compone
.bindTo(input);

// Handle input key events on the search input, triggering results to load.
['input', 'focus'].forEach(ev => {
input.addEventListener(ev as 'input' | 'focus', function() {
['input', 'focus'].forEach((ev) => {
input.addEventListener(ev as 'input' | 'focus', function () {
const query = this.value.toLowerCase();

if (!query) return;
Expand All @@ -247,7 +247,7 @@ export default class Search<T extends SearchAttrs = SearchAttrs> extends Compone
}, 250);
});
});
input.addEventListener('focus', function() {
input.addEventListener('focus', function () {
this.addEventListener('mouseup', (e) => e.preventDefault(), { once: true });
this.select();
});
Expand Down Expand Up @@ -344,7 +344,7 @@ export default class Search<T extends SearchAttrs = SearchAttrs> extends Compone
fixedIndex = 0;
}

items.forEach(el => el.classList.remove('active'));
items.forEach((el) => el.classList.remove('active'));
const item = items[fixedIndex];
const dropdown = item.parentElement!;
item.classList.add('active');
Expand All @@ -371,7 +371,7 @@ export default class Search<T extends SearchAttrs = SearchAttrs> extends Compone
if (typeof scrollTop !== 'undefined') {
dropdown.scrollTo({
top: scrollTop,
behavior: 'smooth'
behavior: 'smooth',
});
}
}
Expand Down
Loading

0 comments on commit 1644f2e

Please sign in to comment.