[Glitch] Add feature to automatically attach quote on eligible link past in Web UI composer

Port cda07686df to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
Claire
2025-10-06 15:43:20 +02:00
parent d5ce785267
commit d34b4f3fd0
3 changed files with 56 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ import { createAction } from '@reduxjs/toolkit';
import type { List as ImmutableList, Map as ImmutableMap } from 'immutable';
import { apiUpdateMedia } from 'flavours/glitch/api/compose';
import { apiGetSearch } from 'flavours/glitch/api/search';
import type { ApiMediaAttachmentJSON } from 'flavours/glitch/api_types/media_attachments';
import type { MediaAttachment } from 'flavours/glitch/models/media_attachment';
import {
@@ -16,6 +17,7 @@ import type { Status } from '../models/status';
import { showAlert } from './alerts';
import { focusCompose } from './compose';
import { importFetchedStatuses } from './importer';
import { openModal } from './modal';
const messages = defineMessages({
@@ -165,6 +167,41 @@ export const quoteComposeById = createAppThunk(
},
);
export const pasteLinkCompose = createDataLoadingThunk(
'compose/pasteLink',
async ({ url }: { url: string }) => {
return await apiGetSearch({
q: url,
type: 'statuses',
resolve: true,
limit: 2,
});
},
(data, { dispatch, getState }) => {
const composeState = getState().compose;
if (
composeState.get('quoted_status_id') ||
composeState.get('is_submitting') ||
composeState.get('poll') ||
composeState.get('is_uploading')
)
return;
dispatch(importFetchedStatuses(data.statuses));
if (
data.statuses.length === 1 &&
data.statuses[0] &&
['automatic', 'manual'].includes(
data.statuses[0].quote_approval?.current_user ?? 'denied',
)
) {
dispatch(quoteComposeById(data.statuses[0].id));
}
},
);
export const quoteComposeCancel = createAction('compose/quoteComposeCancel');
export const setComposeQuotePolicy = createAction<ApiQuotePolicy>(

View File

@@ -150,10 +150,7 @@ const AutosuggestTextarea = forwardRef(({
}, [suggestions, onSuggestionSelected, textareaRef]);
const handlePaste = useCallback((e) => {
if (e.clipboardData && e.clipboardData.files.length === 1) {
onPaste(e.clipboardData.files);
e.preventDefault();
}
onPaste(e);
}, [onPaste]);
// Show the suggestions again whenever they change and the textarea is focused

View File

@@ -10,11 +10,14 @@ import {
insertEmojiCompose,
uploadCompose,
} from 'flavours/glitch/actions/compose';
import { pasteLinkCompose } from 'flavours/glitch/actions/compose_typed';
import { openModal } from 'flavours/glitch/actions/modal';
import { privacyPreference } from 'flavours/glitch/utils/privacy_preference';
import ComposeForm from '../components/compose_form';
const urlLikeRegex = /^https?:\/\/[^\s]+\/[^\s]+$/i;
const sideArmPrivacy = state => {
const inReplyTo = state.getIn(['compose', 'in_reply_to']);
const replyPrivacy = inReplyTo ? state.getIn(['statuses', inReplyTo, 'visibility']) : null;
@@ -93,8 +96,21 @@ const mapDispatchToProps = (dispatch, props) => ({
dispatch(changeComposeSpoilerText(checked));
},
onPaste (files) {
dispatch(uploadCompose(files));
onPaste (e) {
if (e.clipboardData && e.clipboardData.files.length === 1) {
dispatch(uploadCompose(e.clipboardData.files));
e.preventDefault();
} else if (e.clipboardData && e.clipboardData.files.length === 0) {
const data = e.clipboardData.getData('text/plain');
if (!data.match(urlLikeRegex)) return;
try {
const url = new URL(data);
dispatch(pasteLinkCompose({ url }));
} catch {
return;
}
}
},
onPickEmoji (position, data, needsSpace) {