Default to whatever content type is supported

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-10-21 20:39:23 +02:00
parent 34d68cf2d8
commit a3c5993797
19 changed files with 66 additions and 38 deletions

View file

@ -87,7 +87,7 @@ const baseAccountSchema = v.object({
noindex: v.fallback(v.nullable(v.boolean()), null),
suspended: v.fallback(v.optional(v.boolean()), undefined),
limited: v.fallback(v.optional(v.boolean()), undefined),
created_at: v.fallback(datetimeSchema, new Date().toUTCString()),
created_at: v.fallback(datetimeSchema, new Date().toISOString()),
last_status_at: v.fallback(v.nullable(v.pipe(v.string(), v.isoDate())), null),
statuses_count: v.fallback(v.number(), 0),
followers_count: v.fallback(v.number(), 0),

View file

@ -7,7 +7,7 @@ import { datetimeSchema, filteredArray } from '../utils';
import { adminIpSchema } from './ip';
/** @see {@link https://docs.joinmastodon.org/entities/Admin_Account/} */
const adminAccountSchema = v.pipe(
const adminAccountSchema = v.pipe(
v.any(),
v.transform((account: any) => {
if (!account.account) {

View file

@ -4,7 +4,7 @@ import * as v from 'valibot';
import { announcementSchema } from '../announcement';
/** @see {@link https://docs.pleroma.social/backend/development/API/admin_api/#get-apiv1pleromaadminannouncements} */
const adminAnnouncementSchema = v.pipe(
const adminAnnouncementSchema = v.pipe(
v.any(),
v.transform((announcement: any) => ({
...announcement,

View file

@ -1,6 +1,6 @@
import * as v from 'valibot';
const adminRelaySchema = v.pipe(
const adminRelaySchema = v.pipe(
v.any(),
v.transform((data: any) => ({ id: data.actor, ...data })),
v.object({

View file

@ -8,7 +8,7 @@ import { datetimeSchema, filteredArray } from '../utils';
import { adminAccountSchema } from './account';
/** @see {@link https://docs.joinmastodon.org/entities/Admin_Report/} */
const adminReportSchema = v.pipe(
const adminReportSchema = v.pipe(
v.any(),
v.transform((report: any) => {
if (report.actor) {

View file

@ -203,7 +203,7 @@ const pleromaSchema = coerceObject({
)),
enabled: v.fallback(v.boolean(), false),
}),
post_formats: v.fallback(v.optional(v.array(v.string())), undefined),
post_formats: v.fallback(v.array(v.string()), ['text/plain']),
restrict_unauthenticated: coerceObject({
activities: coerceObject({
local: v.fallback(v.boolean(), false),

View file

@ -7,7 +7,7 @@ const baseRuleSchema = v.object({
});
/** @see {@link https://docs.joinmastodon.org/entities/Rule/} */
const ruleSchema = v.pipe(
const ruleSchema = v.pipe(
v.any(),
v.transform((data: any) => ({
...data,

View file

@ -3,7 +3,7 @@ import * as v from 'valibot';
import { accountSchema } from './account';
import { datetimeSchema } from './utils';
const scrobbleSchema = v.pipe(
const scrobbleSchema = v.pipe(
v.any(),
v.transform((scrobble: any) => scrobble ? {
external_link: scrobble.externalLink,

View file

@ -3,7 +3,7 @@ import * as v from 'valibot';
import { accountSchema } from './account';
/** @see {@link https://docs.joinmastodon.org/entities/Suggestion} */
const suggestionSchema = v.pipe(
const suggestionSchema = v.pipe(
v.any(),
v.transform((suggestion: any) => {
/**

View file

@ -15,7 +15,7 @@ const translationMediaAttachment = v.object({
});
/** @see {@link https://docs.joinmastodon.org/entities/Translation/} */
const translationSchema = v.pipe(
const translationSchema = v.pipe(
v.any(),
v.transform((translation: any) => {
/**

View file

@ -4,7 +4,7 @@ import { blurhashSchema } from './media-attachment';
import { historySchema } from './tag';
/** @see {@link https://docs.joinmastodon.org/entities/PreviewCard/#trends-link} */
const trendsLinkSchema = v.pipe(
const trendsLinkSchema = v.pipe(
v.any(),
v.transform((link: any) => ({ ...link, id: link.url })),
v.object({

View file

@ -1000,6 +1000,7 @@ const getFeatures = (instance: Instance) => {
v.software === PLEROMA,
v.software === MITRA,
v.software === GOTOSOCIAL,
instance.pleroma.metadata.post_formats.length > 1,
]),
/**

View file

@ -1,6 +1,6 @@
{
"name": "pl-api",
"version": "0.1.2",
"version": "0.1.3",
"type": "module",
"homepage": "https://github.com/mkljczk/pl-fe/tree/fork/packages/pl-api",
"repository": {

View file

@ -102,7 +102,7 @@
"mini-css-extract-plugin": "^2.9.1",
"multiselect-react-dropdown": "^2.0.25",
"path-browserify": "^1.0.1",
"pl-api": "^0.1.2",
"pl-api": "^0.1.3",
"postcss": "^8.4.47",
"process": "^0.11.10",
"punycode": "^2.1.1",

View file

@ -29,19 +29,27 @@ const ContentTypeButton: React.FC<IContentTypeButton> = ({ composeId }) => {
const handleChange = (contentType: string) => () => dispatch(changeComposeContentType(composeId, contentType));
const options = [
{
const postFormats = instance.pleroma.metadata.post_formats;
const options = [];
if (postFormats.includes('text/plain')) {
options.push({
icon: require('@tabler/icons/outline/pilcrow.svg'),
text: intl.formatMessage(messages.content_type_plaintext),
value: 'text/plain',
},
{ icon: require('@tabler/icons/outline/markdown.svg'),
});
}
if (postFormats.includes('text/markdown')) {
options.push({
icon: require('@tabler/icons/outline/markdown.svg'),
text: intl.formatMessage(messages.content_type_markdown),
value: 'text/markdown',
},
];
});
}
if (instance.pleroma.metadata.post_formats?.includes('text/html')) {
if (postFormats.includes('text/html')) {
options.push({
icon: require('@tabler/icons/outline/html.svg'),
text: intl.formatMessage(messages.content_type_html),
@ -49,11 +57,13 @@ const ContentTypeButton: React.FC<IContentTypeButton> = ({ composeId }) => {
});
}
options.push({
icon: require('@tabler/icons/outline/text-caption.svg'),
text: intl.formatMessage(messages.content_type_wysiwyg),
value: 'wysiwyg',
});
if (postFormats.includes('text/markdown')) {
options.push({
icon: require('@tabler/icons/outline/text-caption.svg'),
text: intl.formatMessage(messages.content_type_wysiwyg),
value: 'wysiwyg',
});
}
const option = options.find(({ value }) => value === contentType);

View file

@ -9,6 +9,7 @@ import { Mutliselect, SelectDropdown } from 'pl-fe/features/forms';
import SettingToggle from 'pl-fe/features/notifications/components/setting-toggle';
import { useAppDispatch } from 'pl-fe/hooks/useAppDispatch';
import { useFeatures } from 'pl-fe/hooks/useFeatures';
import { useInstance } from 'pl-fe/hooks/useInstance';
import { useSettings } from 'pl-fe/hooks/useSettings';
import ThemeToggle from '../ui/components/theme-toggle';
@ -98,6 +99,7 @@ const Preferences = () => {
const dispatch = useAppDispatch();
const features = useFeatures();
const settings = useSettings();
const instance = useInstance();
const onSelectChange = (event: React.ChangeEvent<HTMLSelectElement>, path: string[]) => {
dispatch(changeSetting(path, event.target.value, { showAlert: true }));
@ -123,11 +125,17 @@ const Preferences = () => {
private: intl.formatMessage(messages.privacy_followers_only),
}), [settings.locale]);
const defaultContentTypeOptions = React.useMemo(() => ({
'text/plain': intl.formatMessage(messages.content_type_plaintext),
'text/markdown': intl.formatMessage(messages.content_type_markdown),
'text/html': intl.formatMessage(messages.content_type_html),
}), [settings.locale]);
const defaultContentTypeOptions = React.useMemo(() => {
const postFormats = instance.pleroma.metadata.post_formats;
const options = Object.entries({
'text/plain': intl.formatMessage(messages.content_type_plaintext),
'text/markdown': intl.formatMessage(messages.content_type_markdown),
'text/html': intl.formatMessage(messages.content_type_html),
}).filter(([key]) => postFormats.includes(key));
if (options.length > 1) return Object.fromEntries(options);
}, [settings.locale]);
return (
<Form>
@ -179,7 +187,7 @@ const Preferences = () => {
</ListItem>
)}
{features.richText && (
{features.richText && !!defaultContentTypeOptions && (
<ListItem label={<FormattedMessage id='preferences.fields.content_type_label' defaultMessage='Default post format' />}>
<SelectDropdown
className='max-w-[200px]'

View file

@ -1,6 +1,7 @@
import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet, Record as ImmutableRecord, fromJS } from 'immutable';
import { PLEROMA, type CredentialAccount, type MediaAttachment, type Tag } from 'pl-api';
import { Instance, PLEROMA, type CredentialAccount, type MediaAttachment, type Tag } from 'pl-api';
import { INSTANCE_FETCH_SUCCESS, InstanceAction } from 'pl-fe/actions/instance';
import { isNativeEmoji } from 'pl-fe/features/emoji';
import { tagHistory } from 'pl-fe/settings';
import { hasIntegerMediaIds } from 'pl-fe/utils/status';
@ -276,6 +277,12 @@ const importAccount = (compose: Compose, account: CredentialAccount) => {
// }
// };
const updateDefaultContentType = (compose: Compose, instance: Instance) => {
const postFormats = instance.pleroma.metadata.post_formats;
return compose.update('content_type', type => postFormats.includes(type) ? type : postFormats.includes('text/markdown') ? 'text/markdown' : postFormats[0]);
};
const updateCompose = (state: State, key: string, updater: (compose: Compose) => Compose) =>
state.update(key, state.get('default')!, updater);
@ -283,7 +290,7 @@ const initialState: State = ImmutableMap({
default: ReducerCompose({ idempotencyKey: crypto.randomUUID(), resetFileKey: getResetFileKey() }),
});
const compose = (state = initialState, action: ComposeAction | EventsAction | MeAction | TimelineAction) => {
const compose = (state = initialState, action: ComposeAction | EventsAction | InstanceAction | MeAction | TimelineAction) => {
switch (action.type) {
case COMPOSE_TYPE_CHANGE:
return updateCompose(state, action.composeId, compose => compose.withMutations(map => {
@ -589,6 +596,8 @@ const compose = (state = initialState, action: ComposeAction | EventsAction | Me
.set('quote', null));
case COMPOSE_FEDERATED_CHANGE:
return updateCompose(state, action.composeId, compose => compose.update('federated', value => !value));
case INSTANCE_FETCH_SUCCESS:
return updateCompose(state, 'default', (compose) => updateDefaultContentType(compose, action.instance));
default:
return state;
}

View file

@ -19,7 +19,7 @@ const settingsSchema = v.object({
deleteModal: v.fallback(v.boolean(), true),
missingDescriptionModal: v.fallback(v.boolean(), true),
defaultPrivacy: v.fallback(v.picklist(['public', 'unlisted', 'private', 'direct']), 'public'),
defaultContentType: v.fallback(v.picklist(['text/plain', 'text/markdown']), 'text/plain'),
defaultContentType: v.fallback(v.picklist(['text/plain', 'text/markdown', 'text/html']), 'text/plain'),
themeMode: v.fallback(v.picklist(['system', 'light', 'dark', 'black']), 'system'),
locale: v.fallback(
v.pipe(

View file

@ -7570,10 +7570,10 @@ pkg-dir@^4.1.0:
dependencies:
find-up "^4.0.0"
pl-api@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.1.2.tgz#08794b017f64c58ce128074afdd2144f715359e2"
integrity sha512-HZNrEDvnL1+8yax7lZwe/vCELme8ieNQB6LzUKTQU8qqhMSk7EdLBeEUQok/csyAu0X+IaSsoWKMA91xYzpuGA==
pl-api@^0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.1.3.tgz#72d434a0ec8d713e5b227b35497da33cf26975a6"
integrity sha512-vcl3aGOy3AocQek3+S97QB0jIcF+iV66FvMqrFB/qnfo3Ryte9dcG6XcDxQ5LpogFQAILKk/Mm5uY2W9RZtwHA==
dependencies:
blurhash "^2.0.5"
http-link-header "^1.1.3"