开门见山地说😎之前我发了大量长毛象魔改的代码,但唯独跳过了增加图片数量。笔者也不想水博客数量,读者应该也认为自己可以触类旁通。但如果试下来,踩的坑也应该和我一样,于是为了能实现glitch的12宫格甚至16宫格,就


不做了,睡大觉,早上起来把It’s MyGO!!!!!和Ave Mujica给补了,然后回来写了这篇博客。
接下来直接上代码

增加图片上限

1
2
3
4
5
6
7
8
9
10
11
12
diff --git a/app/models/status.rb b/app/models/status.rb
index e0630733d8..27fa5fe1f7 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -39,7 +39,7 @@ class Status < ApplicationRecord
include Status::SnapshotConcern
include Status::ThreadingConcern

- MEDIA_ATTACHMENTS_LIMIT = 4
+ MEDIA_ATTACHMENTS_LIMIT = 12

rate_limit by: :account, family: :statuses

这是增加媒体上限的代码。不建议无限提高或者取消上限,因为这代码也决定了本实例接收其他实例媒体文件的上限。

经过此番修改,你发送的12宫格可以给所有不适用官方mastodon的Fediverse实例所看见,但是却不能被自己和自己实例的用户看见。经过更换主题(换成官方默认的主题),可以看见确实有多图。

九宫格的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
diff --git a/app/javascript/mastodon/components/media_gallery.jsx b/app/javascript/mastodon/components/media_gallery.jsx
index 59963a0a9f..3dc0fbe42e 100644
--- a/app/javascript/mastodon/components/media_gallery.jsx
+++ b/app/javascript/mastodon/components/media_gallery.jsx
@@ -16,6 +16,14 @@ import { formatTime } from 'mastodon/features/video';

import { autoPlayGif, displayMedia, useBlurhash } from '../initial_state';

+const colCount = function(size) {
+ return Math.max(Math.ceil(Math.sqrt(size)), 2);
+};
+
+const rowCount = function(size) {
+ return Math.ceil(size / colCount(size));
+};
+
class Item extends PureComponent {

static propTypes = {
@@ -87,14 +95,18 @@ class Item extends PureComponent {
let badges = [], thumbnail;

let width = 50;
- let height = 100;
+ let height = 50;

- if (size === 1) {
- width = 100;
- }
+ const cols = colCount(size);
+ const remaining = (-size % cols + cols) % cols;
+ const largeCount = Math.floor(remaining / 3); // width=2, height=2
+ const mediumCount = remaining % 3; // height=2

- if (size === 4 || (size === 3 && index > 0)) {
- height = 50;
+ if (size === 1 || index < largeCount) {
+ width = 100;
+ height = 100;
+ } else if (size === 2 || index < largeCount + mediumCount) {
+ height = 100;
}

const description = attachment.getIn(['translation', 'description']) || attachment.get('description');
@@ -302,7 +314,12 @@ class MediaGallery extends PureComponent {
if (this.isFullSizeEligible()) {
style.aspectRatio = `${this.props.media.getIn([0, 'meta', 'small', 'aspect'])}`;
} else {
- style.aspectRatio = '3 / 2';
+ const cols = colCount(media.size);
+ const rows = rowCount(media.size);
+ style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
+ style.gridTemplateRows = `repeat(${rows}, 1fr)`;
+
+ style.aspectRatio = '1 / 1';
}

const size = media.size;
diff --git a/app/javascript/mastodon/features/compose/components/upload_form.tsx b/app/javascript/mastodon/features/compose/components/upload_form.tsx
index 1c01c2bd9c..aaff7c13dd 100644
--- a/app/javascript/mastodon/features/compose/components/upload_form.tsx
+++ b/app/javascript/mastodon/features/compose/components/upload_form.tsx
@@ -33,6 +33,9 @@ import { useAppSelector, useAppDispatch } from 'mastodon/store';
import { Upload } from './upload';
import { UploadProgress } from './upload_progress';

+const colCount = (size: number) => Math.max(Math.ceil(Math.sqrt(size)), 2);
+const rowCount = (size: number) => Math.ceil(size / colCount(size));
+
const messages = defineMessages({
screenReaderInstructions: {
id: 'upload_form.drag_and_drop.instructions',
@@ -140,6 +143,15 @@ export const UploadForm: React.FC = () => {
[intl],
);

+ const style = {
+ gridTemplateColumns: '1fr',
+ gridTemplateRows: '1fr',
+ };
+ const cols = colCount(mediaIds.size);
+ const rows = rowCount(mediaIds.size);
+ style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
+ style.gridTemplateRows = `repeat(${rows}, 1fr)`;
+
return (
<>
<UploadProgress
@@ -151,6 +163,7 @@ export const UploadForm: React.FC = () => {
{mediaIds.size > 0 && (
<div
className={`compose-form__uploads media-gallery media-gallery--layout-${mediaIds.size}`}
+ style={style}
>
<DndContext
sensors={sensors}

该布局修改的代码参考了这两个提交。链接我全发在长毛象里了,我刚用全文搜索搜出来,在这用一个链接概括一下。
可以发现,原作者将远程实例的媒体限制与本站的媒体限制分开来,试图实现官方实例的向上兼容,但是直至今日,似乎未被接受。

一点题外话

我发现近两年卖沟和母鸡卡的梗图比当年LL大法还火,就去补了番。其实动画的世界观和长毛象的世界观应该是相通的。这世界上有两类人:一个是婴儿潮(Boomer)一代,他们是50后,吃尽了二战结束以后的人口红利(可能中国的经济红利不如国外);另一群是迷失的一代,苏联解体后的一代,世界观应该跟《新世纪福音战士》(天鹰战士)差不多。机会就这么一点。
不管是已经就职的还是正在求职的,我感觉每天这压力跟断刀大赛的决赛差不多,不管你是摆烂交个钢坯给评委,还是努力了,不管刀最终还是崩碎了,还是花里胡哨地一通操作,最后对手更优秀,你这结果还是跟开局摆烂一样。然后有些人还要对你跟他们年轻时比较,说“奋斗才需要多大力气啊”。
不管是朋友还是货架上的货物,都不会自己长出来。就说这么多了。