-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackup_viewer.js
185 lines (185 loc) · 7.07 KB
/
backup_viewer.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
(() => {
let styles, backupList
const backupPath = app.getPath("userData") + osfs + "backups"
const E = s => $(document.createElement(s))
Plugin.register("backup_viewer", {
title: "Backup Viewer",
icon: "fa-archive",
author: "Ewan Howell",
description: "View the project backups from within Blockbench.",
about: "Add a backup tab to the Blockbench start screen that allows you to view a list of the project backups from within Blockbench.",
tags: ["Backups", "Blockbench"],
version: "1.0.0",
min_version: "4.5.2",
variant: "desktop",
onload() {
styles = Blockbench.addCSS(`
#backup-tabs {
background-color: var(--color-ui);
position: absolute;
top: -6px;
display: flex;
gap: 2px;
}
.backup-tab {
border-bottom: 3px solid transparent;
cursor: pointer;
padding: 4px 20px 0;
font-size: 1.75rem;
}
.backup-tab-active {
border-bottom: 3px solid var(--color-accent);
cursor: initial;
}
.backup-tab:hover {
background-color: var(--color-selected);
}
.backup-tab-active:hover {
background-color: var(--color-ui);
}
.backup-backup-list {
margin-left: 0!important;
display: grid;
}
.backup-list-inactive {
display: none!important;
}
.backup-item {
background-color: var(--color-back);
padding: 10px 20px;
overflow: hidden;
}
.backup-item, .backup-item * {
cursor: pointer;
}
.backup-item:hover {
background-color: var(--color-accent);
color: var(--color-accent_text);
}
.backup-item > i {
display: none;
}
.backup-item > div {
font-size: 16px;
color: var(--color-subtle_text);
position: relative;
}
.backup-item:hover > div {
color: var(--color-bright_ui_text);
}
.backup-item-hidden {
display: none!important;
}
.backup-list-display-list {
display: block;
}
.backup-list-display-list > .backup-item {
background-color: initial;
padding: 0 0 0 12px;
display: flex;
height: 26.39px;
align-items: center;
margin: 2px 0;
}
.backup-list-display-list > .backup-item:hover {
color: var(--color-light);
}
.backup-list-display-list i {
display: initial;
}
.backup-list-display-list h3 {
font-size: 17.6px;
margin: 4px 0 0 4px!important;
padding: 0!important;
}
.backup-list-display-list span {
flex: 1;
}
.backup-list-display-list div::before {
content: "";
position: absolute;
width: 16px;
height: 26px;
margin-left: -20px;
background: linear-gradient(90deg, transparent, var(--color-ui));
}
`)
$(".start_screen_right > h2").css("position", "relative").append(
E("div").attr("id", "backup-tabs").append(
E("div").text("Recent").attr("id", "backup-recent-tab").addClass("backup-tab backup-tab-active"),
E("div").text("Backups").attr("id", "backup-backup-tab").addClass("backup-tab")
)
)
backupList = E("ul").addClass("backup-backup-list backup-list-inactive").insertAfter($(".start_screen_right > ul"))
if ($("#start_screen_view_menu > .tool").eq(1).hasClass("selected")) backupList.addClass("backup-list-display-list")
$(".backup-tab").on("click", e => {
$(".backup-tab-active").removeClass("backup-tab-active")
$(".start_screen_right > ul").addClass("backup-list-inactive")
const tab = $(e.currentTarget).addClass("backup-tab-active")
const selected = tab.attr("id").split("-")[1]
if (tab.attr("id") === "backup-recent-tab") $(".start_screen_right > ul").first().removeClass("backup-list-inactive")
else backupList.removeClass("backup-list-inactive")
})
const files = fs.readdirSync(backupPath)
const backups = []
for (const file of files) if (file.endsWith(".bbmodel")) {
const m = file.match(/^backup_(?<day>\d{1,2})\.(?<month>\d{1,2})\.(?<year>\d\d)_(?<hour>\d{1,2})\.(?<minute>\d{1,2})(?:_(?<name>.+))?\.bbmodel$/)?.groups
if (!m) continue
const date = new Date(Date.parse(`20${m.year}/${m.month}/${m.day} ${m.hour}:${m.minute}`))
backups.push({
name: m.name ?? "untitled",
path: backupPath + osfs + file,
date
})
}
backups.sort((a, b) => b.date - a.date)
$("#start_screen_view_menu > .search_bar").on("click", processSearch)
const searchBar = $("#start_screen_view_menu > .search_bar > input").on("input", processSearch)
const searchText = searchBar.val()
for (const backup of backups) {
const item = E("li").addClass("backup-item").attr({
title: backup.path,
"data-name": backup.name.toLowerCase()
}).append(
E("i").addClass("fa_big icon fa fa-archive"),
E("h3").text(backup.name),
E("span"),
E("div").text(`${backup.date.getDate().toString().padStart(2, 0)}/${(backup.date.getMonth() + 1).toString().padStart(2, 0)}/${(backup.date.getYear() - 100).toString().padStart(2, 0)} ${backup.date.getHours().toString().padStart(2, 0)}:${backup.date.getMinutes().toString().padStart(2, 0)}`)
).on("click", e => Blockbench.read([backup.path], {}, files => loadModelFile(files[0]))).appendTo(backupList)
if (!backup.name.includes(searchText)) item.addClass("backup-item-hidden")
}
$("#start_screen_view_menu > .tool").first().on("click", gridView)
$("#start_screen_view_menu > .tool").eq(1).on("click", listView)
processSearch()
},
onunload() {
$("#start_screen_view_menu > .search_bar").off("click", processSearch)
$("#start_screen_view_menu > .search_bar > input").off("input", processSearch)
$("#start_screen_view_menu > .tool").first().off("click", gridView)
$("#start_screen_view_menu > .tool").eq(1).off("click", listView)
styles.delete()
$("#backup-tabs").remove()
$(".backup-backup-list").remove()
$(".backup-list-inactive").removeClass("backup-list-inactive")
}
})
function processSearch() {
const text = $("#start_screen_view_menu > .search_bar > input").val().toLowerCase()
$(".backup-item").each(function() {
const item = $(this)
if (!item.attr("data-name").includes(text)) item.addClass("backup-item-hidden")
else item.removeClass("backup-item-hidden")
})
}
function gridView() {
backupList.removeClass("backup-list-display-list")
hideRecent()
}
function listView() {
backupList.addClass("backup-list-display-list")
hideRecent()
}
function hideRecent() {
if ($("#backup-backup-tab").hasClass("backup-tab-active")) $(".start_screen_right > ul").first().addClass("backup-list-inactive")
}
})()