Skip to content

Commit 0f48b2d

Browse files
committed
feat:横屏移动
1 parent 9010456 commit 0f48b2d

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

src/utils/helper.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import markdownItRuby from "markdown-it-ruby";
1010
import markdownItSpan from "./markdown-it-span";
1111
import markdownItRemovepre from "./markdown-it-removepre";
1212
import markdownItLinkfoot from "./markdown-it-linkfoot";
13+
import markdownItImageFlow from "./markdown-it-imageflow";
1314
import highlightjs from "./langHighlight";
1415
import markdownLiReplacer from "./markdown-it-li";
1516
import {LAYOUT_ID} from "./constant";
@@ -93,7 +94,8 @@ markdownParserWechat
9394
.use(markdownItRuby) // 注音符号
9495
.use(markdownItImplicitFigures, {figcaption: true}) // 图示
9596
.use(markdownItDeflist) // 定义列表
96-
.use(markdownLiReplacer); // li 标签中加入 p 标签
97+
.use(markdownLiReplacer) // li 标签中加入 p 标签
98+
.use(markdownItImageFlow); // 横屏移动插件
9799

98100
// 普通解析器,代码高亮用highlight
99101
export const markdownParser = new MarkdownIt({
@@ -126,7 +128,8 @@ markdownParser
126128
.use(markdownItRuby) // 注音符号
127129
.use(markdownItImplicitFigures, {figcaption: true}) // 图示
128130
.use(markdownItDeflist) // 定义列表
129-
.use(markdownLiReplacer); // li 标签中加入 p 标签
131+
.use(markdownLiReplacer) // li 标签中加入 p 标签
132+
.use(markdownItImageFlow); // 横屏移动插件
130133

131134
export const replaceStyle = (id, css) => {
132135
const style = document.getElementById(id);

src/utils/markdown-it-imageflow.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const defaultOption = {
2+
limitless: false, // 限制图片数量
3+
limit: 10, // 图片数量上限,
4+
height: 300, // 组件高度
5+
};
6+
7+
const imageFlowPlugin = (md, opt) => {
8+
const options = opt || defaultOption;
9+
const style = {
10+
IF_layer1: `margin:1em;white-space:normal;border:none;padding:0px;overflow:hidden;height:${options.height}px;line-height:${options.height}px`,
11+
IF_layer2: `white-space:nowrap;width:100%;overflow-x:scroll;`,
12+
IF_layer3: `display:inline-block;word-wrap:break-word;white-space:normal;vertical-align:middle;width:100%;`,
13+
IF_image: `display:inline-block;`,
14+
};
15+
16+
const tokenize = (state, silent) => {
17+
let result = false;
18+
let token;
19+
const matchReg = /^<((!\[([^\]])*\]\(([^)])*\)(,?)(\s)*)*)>/;
20+
21+
if (silent) {
22+
return result;
23+
}
24+
if (state.src.charCodeAt(state.pos) !== 0x3c /* < */) {
25+
return result;
26+
}
27+
28+
const match = matchReg.exec(state.src.substr(state.pos));
29+
30+
if (match) {
31+
if (options.limitless) {
32+
result = true;
33+
} else if (match[1].split(/,|\s/).filter((val) => val).length < options.limit) {
34+
result = true;
35+
} else {
36+
return result;
37+
}
38+
39+
token = state.push("imageFlow_start", "imageFlow", 1);
40+
token = state.push("imageFlow_content", "imageFlow", 0);
41+
[, token.content] = match;
42+
token = state.push("imageFlow_end", "imageFlow", -1);
43+
44+
// update position
45+
var newline = state.src.indexOf("\n", state.pos);
46+
if (newline !== -1) {
47+
state.pos = newline;
48+
} else {
49+
state.pos = state.pos + state.posMax + 1;
50+
}
51+
}
52+
53+
return result;
54+
};
55+
56+
md.renderer.rules.imageFlow_start = () => {
57+
return `<section style=${style.IF_layer1}><section style=${style.IF_layer2}>`;
58+
};
59+
md.renderer.rules.imageFlow_end = () => {
60+
return `</section></section>`;
61+
};
62+
md.renderer.rules.imageFlow_content = (tokens, idx) => {
63+
const contents = tokens[idx].content.split(/,|\s/).filter((val) => val);
64+
let wrapperContent = "";
65+
let image;
66+
contents.forEach((content) => {
67+
image = content.split(/\[|\]|\(|\)|!/).filter((val) => val);
68+
wrapperContent += `<section style=${style.IF_layer3}><img alt=${image[0]} src=${image[1]} style=${
69+
style.IF_image
70+
} /></section>`;
71+
});
72+
73+
return wrapperContent;
74+
};
75+
76+
md.inline.ruler.before("image", "imageFlow", tokenize);
77+
};
78+
79+
export default imageFlowPlugin;

0 commit comments

Comments
 (0)