Skip to content

Commit 06960dc

Browse files
committed
fix:处理图片的一致性。
1、去掉超时限制 2、使用 Promise 来保证在处理图片上传并替换内容时顺序一致
1 parent a1756e9 commit 06960dc

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

src/api/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ const config = {
1717
// 默认地址请求地址,可在 .env 开头文件中修改,在 Axios 中使用
1818
// 实例化的使用用到
1919
baseURL: import.meta.env.VITE_API_URL as string,
20-
// 设置超时时间(10s)
21-
timeout: 10000,
2220
// 跨域时候允许携带凭证
2321
withCredentials: true
2422
};

src/views/article/edit/index.tsx

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Search from "./search";
2121
import 'bytemd/dist/index.css';
2222
import 'juejin-markdown-themes/dist/juejin.css';
2323
import "./index.scss";
24+
import { set } from "lodash";
2425

2526
const plugins = [
2627
gfm({
@@ -35,6 +36,13 @@ interface TagValue {
3536
tag: string;
3637
}
3738

39+
interface ImageInfo {
40+
img: string;
41+
alt: string;
42+
src: string;
43+
index: number; // 图片在文本中的位置
44+
}
45+
3846
export interface IFormType {
3947
articleId: number;// 文章id
4048
status: number; // 文章状态
@@ -140,61 +148,73 @@ const ArticleEdit: FC<IProps> = props => {
140148
}
141149

142150
// 如果是外网的图片链接,转成内网的图片链接
143-
const handleReplaceImgUrl = async () => {
144-
const { content } = form;
145-
// 临时变量 content
146-
let contentTemp;
147-
console.log("content vs lastContent", content, lastContent);
151+
const uploadImages = async (newVal: string) => {
152+
let add;
148153
// 如果新的内容以上次转链后的内容开头
149-
if (content.startsWith(lastContent)) {
154+
if (newVal.startsWith(lastContent)) {
150155
// 变化的内容
151-
contentTemp = content.substring(lastContent.length);
152-
} else if (lastContent.startsWith(content)) {
156+
add = newVal.substring(lastContent.length);
157+
} else if (lastContent.startsWith(newVal)) {
153158
// 删掉了一部分内容
154-
setLastContent(content);
159+
setLastContent(newVal);
155160
console.log("删掉了一部分内容", lastContent);
156161
return;
157162
} else {
158-
contentTemp = content;
163+
add = newVal;
159164
}
160165

161166
// 正则表达式
162167
const reg = /!\[(.*?)\]\((.*?)\)/mg;
163168
let match;
164169

165-
while ((match = reg.exec(contentTemp)) !== null) {
170+
let uploadTasks = [];
171+
let imageInfos:ImageInfo[] = []; // 用于存储图片信息和它们在文本中的位置
172+
173+
while ((match = reg.exec(add)) !== null) {
166174
const [img, alt, src] = match;
167175
console.log("img, alt, src", match, img, alt, src);
168176
// 如果是外网的图片链接,转成内网的图片链接
169-
if (src.length > 0 && src.startsWith("http") && src.indexOf("saveError") < 0) {
177+
if (src.length > 0 && src.startsWith("http")
178+
&& src.indexOf("saveError") < 0) {
179+
// 收集图片信息
180+
imageInfos.push({ img, alt, src, index: match.index });
170181
// 判断图片的链接是否已经上传过了
171182
if (!canUpload(src)) {
172183
console.log("30秒内防重复提交,忽略:", src);
173184
continue;
185+
} else {
186+
uploadTasks.push(saveImgApi(src));
174187
}
188+
}
189+
}
190+
191+
// 同时上传所有图片
192+
const results = await Promise.all(uploadTasks);
175193

176-
// 调用上传图片的接口
177-
const { status: resultStatus, result } = await saveImgApi(src);
178-
const { code } = resultStatus || {};
179-
if (code === 0) {
194+
// 替换所有图片链接
195+
let newContent = newVal;
196+
results.forEach((result, i) => {
197+
if (result.status && result.status.code === 0 && result.result) {
180198
// 重新组织图片的路径
181-
let newSrc = `![${alt}](${result?.imagePath})`;
199+
const newSrc = `![${imageInfos[i].alt}](${result.result.imagePath})`;
182200
console.log("newSrc", newSrc);
183201
// 替换后的内容
184-
let newContent = content.replace(img, newSrc);
202+
newContent = newContent.replace(imageInfos[i].img, newSrc);
185203
console.log("newContent", newContent);
186-
187-
// 替换图片链接
188-
setContent(newContent);
189-
handleChange({ content: newContent });
190-
setLastContent(newContent);
191-
} else {
192-
message.error("转链失败");
193204
}
194-
}
195-
}
205+
});
206+
setLastContent(newVal);
196207

197-
message.success("转链完成,可以看看控制台的输出");
208+
return newContent;
209+
}
210+
211+
const handleReplaceImgUrl = async () => {
212+
const { content } = form;
213+
const newContent = await uploadImages(content);
214+
if (newContent) {
215+
setContent(newContent);
216+
handleChange({ content: newContent });
217+
}
198218
}
199219

200220
// 编辑或者新增时提交数据到服务器端

0 commit comments

Comments
 (0)