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

Reduce differences with upstream in “public” pack #2680

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
115 changes: 50 additions & 65 deletions app/javascript/flavours/glitch/packs/public.jsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,32 @@
import 'packs/public-path';
import { createRoot } from 'react-dom/client';

import { IntlMessageFormat } from 'intl-messageformat';
import 'packs/public-path';

import { IntlMessageFormat } from 'intl-messageformat';
import { defineMessages } from 'react-intl';

import Rails from '@rails/ujs';
import axios from 'axios';
import { createBrowserHistory } from 'history';
import { throttle } from 'lodash';

import { timeAgoString } from 'flavours/glitch/components/relative_timestamp';
import emojify from 'flavours/glitch/features/emoji/emoji';
import loadKeyboardExtensions from 'flavours/glitch/load_keyboard_extensions';
import { loadLocale, getLocale } from 'flavours/glitch/locales';
import { loadPolyfills } from 'flavours/glitch/polyfills';
import ready from 'flavours/glitch/ready';

import 'cocoon-js-vanilla';

const messages = defineMessages({
usernameTaken: { id: 'username.taken', defaultMessage: 'That username is taken. Try another' },
passwordExceedsLength: { id: 'password_confirmation.exceeds_maxlength', defaultMessage: 'Password confirmation exceeds the maximum password length' },
passwordDoesNotMatch: { id: 'password_confirmation.mismatching', defaultMessage: 'Password confirmation does not match' },
});

function main() {
function loaded() {
const { messages: localeData } = getLocale();

const scrollToDetailedStatus = () => {
const history = createBrowserHistory();
const detailedStatuses = document.querySelectorAll('.public-layout .detailed-status');
const location = history.location;

if (detailedStatuses.length === 1 && (!location.state || !location.state.scrolledToDetailedStatus)) {
detailedStatuses[0].scrollIntoView();
history.replace(location.pathname, { ...location.state, scrolledToDetailedStatus: true });
}
};

const getEmojiAnimationHandler = (swapTo) => {
return ({ target }) => {
target.src = target.getAttribute(swapTo);
};
};

const locale = document.documentElement.lang;

const dateTimeFormat = new Intl.DateTimeFormat(locale, {
Expand Down Expand Up @@ -118,6 +104,7 @@ function main() {
});

const reactComponents = document.querySelectorAll('[data-component]');

if (reactComponents.length > 0) {
import(/* webpackChunkName: "containers/media_container" */ 'flavours/glitch/containers/media_container')
.then(({ default: MediaContainer }) => {
Expand All @@ -132,27 +119,21 @@ function main() {
const root = createRoot(content);
root.render(<MediaContainer locale={locale} components={reactComponents} />);
document.body.appendChild(content);
scrollToDetailedStatus();
})
.catch(error => {
console.error(error);
scrollToDetailedStatus();
});
} else {
scrollToDetailedStatus();
}

Rails.delegate(document, '#user_account_attributes_username', 'input', throttle(() => {
const username = document.getElementById('user_account_attributes_username');

if (username.value && username.value.length > 0) {
axios.get('/api/v1/accounts/lookup', { params: { acct: username.value } }).then(() => {
username.setCustomValidity(formatMessage(messages.usernameTaken));
Rails.delegate(document, '#user_account_attributes_username', 'input', throttle(({ target }) => {
if (target.value && target.value.length > 0) {
axios.get('/api/v1/accounts/lookup', { params: { acct: target.value } }).then(() => {
target.setCustomValidity(formatMessage(messages.usernameTaken));
}).catch(() => {
username.setCustomValidity('');
target.setCustomValidity('');
});
} else {
username.setCustomValidity('');
target.setCustomValidity('');
}
}, 500, { leading: false, trailing: true }));

Expand All @@ -170,9 +151,6 @@ function main() {
}
});

Rails.delegate(document, '.custom-emoji', 'mouseover', getEmojiAnimationHandler('data-original'));
Rails.delegate(document, '.custom-emoji', 'mouseout', getEmojiAnimationHandler('data-static'));

Rails.delegate(document, '.status__content__spoiler-link', 'click', function() {
const statusEl = this.parentNode.parentNode;

Expand All @@ -192,44 +170,51 @@ function main() {
const message = (statusEl.dataset.spoiler === 'expanded') ? (localeData['status.show_less'] || 'Show less') : (localeData['status.show_more'] || 'Show more');
spoilerLink.textContent = (new IntlMessageFormat(message, locale)).format();
});
}

const toggleSidebar = () => {
const sidebar = document.querySelector('.sidebar ul');
const toggleButton = document.querySelector('.sidebar__toggle__icon');
const toggleSidebar = () => {
const sidebar = document.querySelector('.sidebar ul');
const toggleButton = document.querySelector('.sidebar__toggle__icon');

if (sidebar.classList.contains('visible')) {
document.body.style.overflow = null;
toggleButton.setAttribute('aria-expanded', 'false');
} else {
document.body.style.overflow = 'hidden';
toggleButton.setAttribute('aria-expanded', 'true');
}
if (sidebar.classList.contains('visible')) {
document.body.style.overflow = null;
toggleButton.setAttribute('aria-expanded', 'false');
} else {
document.body.style.overflow = 'hidden';
toggleButton.setAttribute('aria-expanded', 'true');
}

toggleButton.classList.toggle('active');
sidebar.classList.toggle('visible');
};
toggleButton.classList.toggle('active');
sidebar.classList.toggle('visible');
};

Rails.delegate(document, '.sidebar__toggle__icon', 'click', () => {
toggleSidebar();
});

Rails.delegate(document, '.sidebar__toggle__icon', 'click', () => {
Rails.delegate(document, '.sidebar__toggle__icon', 'keydown', e => {
if (e.key === ' ' || e.key === 'Enter') {
e.preventDefault();
toggleSidebar();
});
}
});

Rails.delegate(document, '.custom-emoji', 'mouseover', ({ target }) => target.src = target.getAttribute('data-original'));
Rails.delegate(document, '.custom-emoji', 'mouseout', ({ target }) => target.src = target.getAttribute('data-static'));

Rails.delegate(document, '.sidebar__toggle__icon', 'keydown', e => {
if (e.key === ' ' || e.key === 'Enter') {
e.preventDefault();
toggleSidebar();
// Empty the honeypot fields in JS in case something like an extension
// automatically filled them.
Rails.delegate(document, '#registration_new_user,#new_user', 'submit', () => {
['user_website', 'user_confirm_password', 'registration_user_website', 'registration_user_confirm_password'].forEach(id => {
const field = document.getElementById(id);
if (field) {
field.value = '';
}
});
});

// Empty the honeypot fields in JS in case something like an extension
// automatically filled them.
Rails.delegate(document, '#registration_new_user,#new_user', 'submit', () => {
['user_website', 'user_confirm_password', 'registration_user_website', 'registration_user_confirm_password'].forEach(id => {
const field = document.getElementById(id);
if (field) {
field.value = '';
}
});
});
function main() {
ready(loaded);
}

loadPolyfills()
Expand Down
42 changes: 0 additions & 42 deletions app/javascript/flavours/glitch/packs/settings.js

This file was deleted.

2 changes: 1 addition & 1 deletion app/javascript/flavours/glitch/theme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pack:
mailer:
modal:
public: packs/public.jsx
settings: packs/settings.js
settings: packs/public.jsx
sign_up: packs/sign_up.js
share: packs/share.jsx

Expand Down
4 changes: 2 additions & 2 deletions app/javascript/packs/public.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './public-path';

import { createRoot } from 'react-dom/client';

import './public-path';

import { IntlMessageFormat } from 'intl-messageformat';
import { defineMessages } from 'react-intl';

Expand Down