-
-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathresize_image.js
More file actions
147 lines (132 loc) · 5.44 KB
/
resize_image.js
File metadata and controls
147 lines (132 loc) · 5.44 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class ResizeImagePlugin extends BasePlugin {
init = () => {
this.checkers = Object.entries(this.config.MODIFIER_KEY)
.filter(([_, modifier]) => Boolean(modifier))
.map(([type, modifier]) => ({ type, check: this.utils.modifierKey(modifier) }))
}
process = () => {
this.utils.settings.autoSave(this)
this.toggleRecorder(false)
this.toggleResizer(true)
}
toggleResizer = (enable = true) => {
const fn = enable ? "addEventListener" : "removeEventListener"
this.utils.entities.eWrite[fn]("wheel", this.onWheel, { passive: false, capture: true })
}
toggleRecorder = (needChange = true) => {
if (needChange) {
this.config.RECORD_RESIZE = !this.config.RECORD_RESIZE
}
if (this.config.RECORD_RESIZE) {
this.utils.stateRecorder.register({
name: this.fixedName,
selector: "#write img",
stateGetter: el => el.style.cssText,
stateRestorer: (el, state) => el.style = state
})
} else {
this.utils.stateRecorder.unregister(this.fixedName)
}
}
onWheel = ev => {
const zoom = this.checkers.find(c => c.check(ev))
if (!zoom) return
const target = ev.target.closest("img")
if (!target) return
ev.preventDefault()
ev.stopPropagation()
const fn = zoom.type === "TEMPORARY" ? this.zoomTemporary : this.zoomPersistent
fn(target, ev.deltaY > 0)
}
resetImageSize = () => {
this.config.ALLOW_EXCEED_LIMIT = !this.config.ALLOW_EXCEED_LIMIT
if (!this.config.ALLOW_EXCEED_LIMIT) {
this.utils.entities.querySelectorAllInWrite("img").forEach(img => {
if (img.style.maxWidth) {
const maxSize = img.parentElement.offsetWidth
if (this.getWidth(img) > maxSize) {
img.style.removeProperty("width")
}
img.style.removeProperty("maxWidth")
}
img.style.removeProperty("left")
img.style.removeProperty("position")
})
}
}
getWidth = img => img.style.width ? parseFloat(img.style.width) : img.getBoundingClientRect().width
setAlign = (align, img, maxWidth) => {
img.setAttribute("align", align)
if (!maxWidth) {
maxWidth = img.parentElement.offsetWidth
}
img.style.marginRight = ""
img.style.marginLeft = ""
if (align !== "center") {
const width = this.getWidth(img)
const margin = (align === "left") ? "marginRight" : "marginLeft"
img.style[margin] = maxWidth - width + "px"
}
}
zoomTemporary = (img, zoomOut, scale = 0.1) => {
let width = this.getWidth(img)
width = zoomOut ? width * (1 - scale) : width * (1 + scale)
const maxWidth = img.parentElement.offsetWidth
img.style.maxWidth = ""
if (!this.config.ALLOW_EXCEED_LIMIT || width <= maxWidth) {
width = Math.min(width, maxWidth)
img.style.width = width + "px"
this.setAlign(this.config.IMAGE_ALIGN, img, maxWidth)
} else {
Object.assign(img.style, {
position: "relative",
width: width + "px",
maxWidth: width + "px",
left: (maxWidth - width) / 2 + "px",
})
}
}
zoomPersistent = (img, zoomOut, scale = 5) => {
const originZoom = img.style.zoom || "100%"
const nextZoom = Math.max(10, Math.min(parseInt(originZoom) + (zoomOut ? -scale : scale), 200)) + "%"
Object.assign(img.style, { position: "", width: "", maxWidth: "", left: "" })
const $span = $(img.closest(".md-image.md-img-loaded"))
if ($span.length === 1) {
File.editor.imgEdit.zoomAction($span, nextZoom)
}
}
getDynamicActions = (anchorNode, meta) => {
const other = [
{ act_value: "zoom_out_20_percent", act_hidden: true },
{ act_value: "zoom_in_20_percent", act_hidden: true },
{ act_value: "set_align_left", act_hidden: true },
{ act_value: "set_align_center", act_hidden: true },
{ act_value: "set_align_right", act_hidden: true },
]
const actions = this.i18n.fillActions([
{ act_value: "record_resize_state", act_state: this.config.RECORD_RESIZE },
{ act_value: "allow_exceed_limit", act_state: this.config.ALLOW_EXCEED_LIMIT },
...other
])
const img = anchorNode.closest("#write .md-image")?.querySelector("img")
if (!img) return actions
meta.target = img
other.forEach(a => a.act_hidden = false)
return actions
}
call = (action, meta) => {
const fnMap = {
record_resize_state: () => this.toggleRecorder(),
allow_exceed_limit: () => this.resetImageSize(),
zoom_out_20_percent: meta => this.zoomTemporary(meta.target, true, 0.2),
zoom_in_20_percent: meta => this.zoomTemporary(meta.target, false, 0.2),
set_align_left: meta => this.setAlign("left", meta.target),
set_align_center: meta => this.setAlign("center", meta.target),
set_align_right: meta => this.setAlign("right", meta.target),
}
fnMap[action]?.(meta)
}
}
module.exports = {
plugin: ResizeImagePlugin
}