Fix lint issues in post form component (#8619)

* fix(client): fix lint issues in post form

* Apply review suggestions from @Johann150

Co-authored-by: Johann150 <johann@qwertqwefsday.eu>

Co-authored-by: Johann150 <johann@qwertqwefsday.eu>
This commit is contained in:
Andreas Nedbal 2022-05-07 10:01:01 +02:00 committed by GitHub
parent 7bd45e5729
commit a29ff7b1fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 48 additions and 49 deletions

View File

@ -228,7 +228,7 @@ if (props.mention) {
text += ' '; text += ' ';
} }
if (props.reply && (props.reply.user.username != $i.username || (props.reply.user.host != null && props.reply.user.host != host))) { if (props.reply && (props.reply.user.username !== $i.username || (props.reply.user.host != null && props.reply.user.host !== host))) {
text = `@${props.reply.user.username}${props.reply.user.host != null ? '@' + toASCII(props.reply.user.host) : ''} `; text = `@${props.reply.user.username}${props.reply.user.host != null ? '@' + toASCII(props.reply.user.host) : ''} `;
} }
@ -239,16 +239,15 @@ if (props.reply && props.reply.text != null) {
for (const x of extractMentions(ast)) { for (const x of extractMentions(ast)) {
const mention = x.host ? const mention = x.host ?
`@${x.username}@${toASCII(x.host)}` : `@${x.username}@${toASCII(x.host)}` :
(otherHost == null || otherHost == host) ? (otherHost == null || otherHost === host) ?
`@${x.username}` : `@${x.username}` :
`@${x.username}@${toASCII(otherHost)}`; `@${x.username}@${toASCII(otherHost)}`;
// //
if ($i.username == x.username && x.host == null) continue; if ($i.username === x.username && (x.host == null || x.host === host)) continue;
if ($i.username == x.username && x.host == host) continue;
// //
if (text.indexOf(`${mention} `) != -1) continue; if (text.includes(`${mention} `)) continue;
text += `${mention} `; text += `${mention} `;
} }
@ -303,7 +302,7 @@ function checkMissingMention() {
const ast = mfm.parse(text); const ast = mfm.parse(text);
for (const x of extractMentions(ast)) { for (const x of extractMentions(ast)) {
if (!visibleUsers.some(u => (u.username === x.username) && (u.host == x.host))) { if (!visibleUsers.some(u => (u.username === x.username) && (u.host === x.host))) {
hasNotSpecifiedMentions = true; hasNotSpecifiedMentions = true;
return; return;
} }
@ -316,7 +315,7 @@ function addMissingMention() {
const ast = mfm.parse(text); const ast = mfm.parse(text);
for (const x of extractMentions(ast)) { for (const x of extractMentions(ast)) {
if (!visibleUsers.some(u => (u.username === x.username) && (u.host == x.host))) { if (!visibleUsers.some(u => (u.username === x.username) && (u.host === x.host))) {
os.api('users/show', { username: x.username, host: x.host }).then(user => { os.api('users/show', { username: x.username, host: x.host }).then(user => {
visibleUsers.push(user); visibleUsers.push(user);
}); });
@ -357,7 +356,7 @@ function chooseFileFrom(ev) {
} }
function detachFile(id) { function detachFile(id) {
files = files.filter(x => x.id != id); files = files.filter(x => x.id !== id);
} }
function updateFiles(_files) { function updateFiles(_files) {
@ -427,24 +426,24 @@ function clear() {
quoteId = null; quoteId = null;
} }
function onKeydown(e: KeyboardEvent) { function onKeydown(ev: KeyboardEvent) {
if ((e.which === 10 || e.which === 13) && (e.ctrlKey || e.metaKey) && canPost) post(); if ((ev.which === 10 || ev.which === 13) && (ev.ctrlKey || ev.metaKey) && canPost) post();
if (e.which === 27) emit('esc'); if (ev.which === 27) emit('esc');
typing(); typing();
} }
function onCompositionUpdate(e: CompositionEvent) { function onCompositionUpdate(ev: CompositionEvent) {
imeText = e.data; imeText = ev.data;
typing(); typing();
} }
function onCompositionEnd(e: CompositionEvent) { function onCompositionEnd(ev: CompositionEvent) {
imeText = ''; imeText = '';
} }
async function onPaste(e: ClipboardEvent) { async function onPaste(ev: ClipboardEvent) {
for (const { item, i } of Array.from(e.clipboardData.items).map((item, i) => ({item, i}))) { for (const { item, i } of Array.from(ev.clipboardData.items).map((item, i) => ({item, i}))) {
if (item.kind == 'file') { if (item.kind === 'file') {
const file = item.getAsFile(); const file = item.getAsFile();
const lio = file.name.lastIndexOf('.'); const lio = file.name.lastIndexOf('.');
const ext = lio >= 0 ? file.name.slice(lio) : ''; const ext = lio >= 0 ? file.name.slice(lio) : '';
@ -453,10 +452,10 @@ async function onPaste(e: ClipboardEvent) {
} }
} }
const paste = e.clipboardData.getData('text'); const paste = ev.clipboardData.getData('text');
if (!props.renote && !quoteId && paste.startsWith(url + '/notes/')) { if (!props.renote && !quoteId && paste.startsWith(url + '/notes/')) {
e.preventDefault(); ev.preventDefault();
os.confirm({ os.confirm({
type: 'info', type: 'info',
@ -472,49 +471,49 @@ async function onPaste(e: ClipboardEvent) {
} }
} }
function onDragover(e) { function onDragover(ev) {
if (!e.dataTransfer.items[0]) return; if (!ev.dataTransfer.items[0]) return;
const isFile = e.dataTransfer.items[0].kind == 'file'; const isFile = ev.dataTransfer.items[0].kind === 'file';
const isDriveFile = e.dataTransfer.types[0] == _DATA_TRANSFER_DRIVE_FILE_; const isDriveFile = ev.dataTransfer.types[0] === _DATA_TRANSFER_DRIVE_FILE_;
if (isFile || isDriveFile) { if (isFile || isDriveFile) {
e.preventDefault(); ev.preventDefault();
draghover = true; draghover = true;
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move'; ev.dataTransfer.dropEffect = ev.dataTransfer.effectAllowed === 'all' ? 'copy' : 'move';
} }
} }
function onDragenter(e) { function onDragenter(ev) {
draghover = true; draghover = true;
} }
function onDragleave(e) { function onDragleave(ev) {
draghover = false; draghover = false;
} }
function onDrop(e): void { function onDrop(ev): void {
draghover = false; draghover = false;
// //
if (e.dataTransfer.files.length > 0) { if (ev.dataTransfer.files.length > 0) {
e.preventDefault(); ev.preventDefault();
for (const x of Array.from(e.dataTransfer.files)) upload(x); for (const x of Array.from(ev.dataTransfer.files)) upload(x);
return; return;
} }
//#region //#region
const driveFile = e.dataTransfer.getData(_DATA_TRANSFER_DRIVE_FILE_); const driveFile = ev.dataTransfer.getData(_DATA_TRANSFER_DRIVE_FILE_);
if (driveFile != null && driveFile != '') { if (driveFile != null && driveFile !== '') {
const file = JSON.parse(driveFile); const file = JSON.parse(driveFile);
files.push(file); files.push(file);
e.preventDefault(); ev.preventDefault();
} }
//#endregion //#endregion
} }
function saveDraft() { function saveDraft() {
const data = JSON.parse(localStorage.getItem('drafts') || '{}'); const draftData = JSON.parse(localStorage.getItem('drafts') || '{}');
data[draftKey] = { draftData[draftKey] = {
updatedAt: new Date(), updatedAt: new Date(),
data: { data: {
text: text, text: text,
@ -527,20 +526,20 @@ function saveDraft() {
} }
}; };
localStorage.setItem('drafts', JSON.stringify(data)); localStorage.setItem('drafts', JSON.stringify(draftData));
} }
function deleteDraft() { function deleteDraft() {
const data = JSON.parse(localStorage.getItem('drafts') || '{}'); const draftData = JSON.parse(localStorage.getItem('drafts') || '{}');
delete data[draftKey]; delete draftData[draftKey];
localStorage.setItem('drafts', JSON.stringify(data)); localStorage.setItem('drafts', JSON.stringify(draftData));
} }
async function post() { async function post() {
let data = { let postData = {
text: text == '' ? undefined : text, text: text === '' ? undefined : text,
fileIds: files.length > 0 ? files.map(f => f.id) : undefined, fileIds: files.length > 0 ? files.map(f => f.id) : undefined,
replyId: props.reply ? props.reply.id : undefined, replyId: props.reply ? props.reply.id : undefined,
renoteId: props.renote ? props.renote.id : quoteId ? quoteId : undefined, renoteId: props.renote ? props.renote.id : quoteId ? quoteId : undefined,
@ -549,18 +548,18 @@ async function post() {
cw: useCw ? cw || '' : undefined, cw: useCw ? cw || '' : undefined,
localOnly: localOnly, localOnly: localOnly,
visibility: visibility, visibility: visibility,
visibleUserIds: visibility == 'specified' ? visibleUsers.map(u => u.id) : undefined, visibleUserIds: visibility === 'specified' ? visibleUsers.map(u => u.id) : undefined,
}; };
if (withHashtags && hashtags && hashtags.trim() !== '') { if (withHashtags && hashtags && hashtags.trim() !== '') {
const hashtags_ = hashtags.trim().split(' ').map(x => x.startsWith('#') ? x : '#' + x).join(' '); const hashtags_ = hashtags.trim().split(' ').map(x => x.startsWith('#') ? x : '#' + x).join(' ');
data.text = data.text ? `${data.text} ${hashtags_}` : hashtags_; postData.text = postData.text ? `${postData.text} ${hashtags_}` : hashtags_;
} }
// plugin // plugin
if (notePostInterruptors.length > 0) { if (notePostInterruptors.length > 0) {
for (const interruptor of notePostInterruptors) { for (const interruptor of notePostInterruptors) {
data = await interruptor.handler(JSON.parse(JSON.stringify(data))); postData = await interruptor.handler(JSON.parse(JSON.stringify(postData)));
} }
} }
@ -572,13 +571,13 @@ async function post() {
} }
posting = true; posting = true;
os.api('notes/create', data, token).then(() => { os.api('notes/create', postData, token).then(() => {
clear(); clear();
nextTick(() => { nextTick(() => {
deleteDraft(); deleteDraft();
emit('posted'); emit('posted');
if (data.text && data.text != '') { if (postData.text && postData.text !== '') {
const hashtags_ = mfm.parse(data.text).filter(x => x.type === 'hashtag').map(x => x.props.hashtag); const hashtags_ = mfm.parse(postData.text).filter(x => x.type === 'hashtag').map(x => x.props.hashtag);
const history = JSON.parse(localStorage.getItem('hashtags') || '[]') as string[]; const history = JSON.parse(localStorage.getItem('hashtags') || '[]') as string[];
localStorage.setItem('hashtags', JSON.stringify(unique(hashtags_.concat(history)))); localStorage.setItem('hashtags', JSON.stringify(unique(hashtags_.concat(history))));
} }
@ -662,7 +661,7 @@ onMounted(() => {
cw = draft.data.cw; cw = draft.data.cw;
visibility = draft.data.visibility; visibility = draft.data.visibility;
localOnly = draft.data.localOnly; localOnly = draft.data.localOnly;
files = (draft.data.files || []).filter(e => e); files = (draft.data.files || []).filter(draftFile => draftFile);
if (draft.data.poll) { if (draft.data.poll) {
poll = draft.data.poll; poll = draft.data.poll;
} }