Skip to content

Commit

Permalink
Fix initialization in SPA frameworks, fix comment URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
adanski committed Feb 1, 2023
1 parent a3c2fc6 commit abce286
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/comments-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class CommentsElement extends HTMLElement implements WebComponent {

connectedCallback(): void {
if (!Object.keys(this.#options).length) {
throw new Error('ax-comments options not set, element could not be initialized.');
return;
}
this.#initServices();
this.#initElement();
Expand All @@ -66,12 +66,14 @@ export class CommentsElement extends HTMLElement implements WebComponent {
}

set options(options: CommentsOptions) {
if (!options) return;
if (Object.keys(this.#options).length) {
console.warn(`<ax-comments> Options already set, component can not be reinitialized.`);
return;
}
Object.assign(this.#options, getDefaultOptions(), options);
Object.freeze(this.#options);
this.connectedCallback();
}

/**
Expand Down Expand Up @@ -305,7 +307,7 @@ export class CommentsElement extends HTMLElement implements WebComponent {
// Hide control row and close button
const mainControlRow: HTMLElement = mainCommentingField.querySelector('.control-row')!;
hideElement(mainControlRow);
hideElement(mainCommentingField.querySelector<HTMLElement>('.close')!);
hideElement(mainCommentingField.querySelector<HTMLElement>('.close'));

// Navigation bar
this.container.append(NavigationElement.create({
Expand Down
20 changes: 11 additions & 9 deletions src/html-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export function getHostContainer(child: HTMLElement): HTMLElement {
return container;
}

export function showElement(element: HTMLElement): void {
if (getElementStyle(element, 'display') === 'none') {
export function showElement(element: HTMLElement | null): void {
if (element && getElementStyle(element, 'display') === 'none') {
showElementUnconditionally(element);
}
}
Expand All @@ -18,8 +18,8 @@ function showElementUnconditionally(element: HTMLElement): void {
element.style.display = PREVIOUS_DISPLAY_VALUE.get(element) || 'block';
}

export function hideElement(element: HTMLElement): void {
if (getElementStyle(element, 'display') !== 'none') {
export function hideElement(element: HTMLElement | null): void {
if (element && getElementStyle(element, 'display') !== 'none') {
hideElementUnconditionally(element);
}
}
Expand All @@ -29,8 +29,10 @@ function hideElementUnconditionally(element: HTMLElement): void {
element.style.display = 'none';
}

export function toggleElementVisibility(element: HTMLElement): void {
if (getElementStyle(element, 'display') !== 'none') {
export function toggleElementVisibility(element: HTMLElement | null): void {
if (!element) {
return;
} else if (getElementStyle(element, 'display') !== 'none') {
hideElementUnconditionally(element);
} else {
showElementUnconditionally(element);
Expand All @@ -44,11 +46,11 @@ export function getElementStyle(element: HTMLElement, prop: StringProps<CSSStyle
type StringProps<T> = ({ [P in keyof T]: T[P] extends string ? P : never })[keyof T];

// Inspired by jQuery
export function isElementVisible(element: HTMLElement): boolean {
return !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
export function isElementVisible(element: HTMLElement | null): boolean {
return !!element && !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
}

export function isElementHidden(element: HTMLElement): boolean {
export function isElementHidden(element: HTMLElement | null): boolean {
return !isElementVisible(element);
}

Expand Down
4 changes: 2 additions & 2 deletions src/subcomponent/comment-container-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class CommentContainerElement extends HTMLElement implements WebComponent

// Time
const commentLink: HTMLAnchorElement = document.createElement('a');
commentLink.href = `#comment-${this.commentModel.id}`;
commentLink.href = `${document.location.href}#comment-${this.commentModel.id}`;
commentLink.textContent = this.#options.timeFormatter(this.commentModel.createdAt);
const time: HTMLTimeElement = document.createElement('time');
time.setAttribute('title', this.commentModel.createdAt.toLocaleString());
Expand Down Expand Up @@ -112,7 +112,7 @@ export class CommentContainerElement extends HTMLElement implements WebComponent
replyTo.setAttribute('data-user-id', parent.creatorUserId);
const parentLink: HTMLAnchorElement = document.createElement('a');
parentLink.textContent = parent.creatorDisplayName || parent.creatorUserId;
parentLink.href = `#comment-${parent.id}`;
parentLink.href = `${document.location.href}#comment-${parent.id}`;

// reply icon
const replyIcon: HTMLElement = document.createElement('i');
Expand Down
6 changes: 3 additions & 3 deletions src/subcomponent/commenting-field-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ export class CommentingFieldElement extends HTMLElement implements WebComponent
const mainTextarea: HTMLElement = e.currentTarget as HTMLElement;
findSiblingsBySelector(mainTextarea, '.control-row')
.forEach(showElement)
showElement(mainTextarea.parentElement!.querySelector('.close')!);
hideElement(mainTextarea.parentElement!.querySelector('.upload.inline-button')!);
showElement(mainTextarea.parentElement!.querySelector('.close'));
hideElement(mainTextarea.parentElement!.querySelector('.upload.inline-button'));
mainTextarea.focus();
};

Expand All @@ -207,7 +207,7 @@ export class CommentingFieldElement extends HTMLElement implements WebComponent

hideElement(mainControlRow);
hideElement(closeButton);
showElement(mainTextarea.parentElement!.querySelector('.upload.inline-button')!);
showElement(mainTextarea.parentElement!.querySelector('.upload.inline-button'));
mainTextarea.blur();
this.onClosed();
};
Expand Down
4 changes: 2 additions & 2 deletions src/subcomponent/navigation-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ export class NavigationElement extends HTMLElement implements WebComponent {
#showMenuDropdown: (e: UIEvent) => void = e => {
if (!this.#dropdownShown) {
e.stopPropagation();
showElement(this.querySelector<HTMLElement>('menu.dropdown')!);
showElement(this.querySelector<HTMLElement>('menu.dropdown'));
this.#dropdownShown = true;
}
};

#hideMenuDropdown: (e: UIEvent) => void = e => {
if (this.#dropdownShown) {
hideElement(this.querySelector<HTMLElement>('menu.dropdown')!);
hideElement(this.querySelector<HTMLElement>('menu.dropdown'));
this.#dropdownShown = false;
}
};
Expand Down

0 comments on commit abce286

Please sign in to comment.