Skip to content

Commit a094757

Browse files
committed
add the ability to import/export settings
1 parent b614b0a commit a094757

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

CHANGELOG.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ v2.0.0
55
- rewrite webserver
66
- add doc presets for autohotkey, discord.dev, discord.sex, flowlauncher.com, lua, mdn, and qmk.
77
- Add the ability to choose a custom port
8-
- Add an auto doctype option for the plugin to try and detect if it can index a doc
8+
- Add an auto doctype option for the plugin to try and detect if it can index a doc
9+
- Add the ability to import/export settings

plugin/server/core.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import base64
45
import json
56
import logging
67
import os
78
from typing import TYPE_CHECKING, Callable
89

910
import aiohttp_jinja2
1011
import jinja2
12+
import msgspec
1113
from aiohttp import web
1214

1315
from ..libraries import doc_types, preset_docs
@@ -32,6 +34,27 @@
3234
}
3335

3436

37+
class PartialLibrary(msgspec.Struct):
38+
name: str
39+
type: str
40+
loc: str | None
41+
use_cache: bool
42+
43+
def to_dict(self):
44+
return {
45+
"name": self.name,
46+
"type": self.type,
47+
"loc": self.loc,
48+
"use_cache": self.use_cache,
49+
}
50+
51+
52+
class ImportExportSettings(msgspec.Struct):
53+
port: int
54+
keyword: str
55+
libraries: list[PartialLibrary]
56+
57+
3558
def build_app(
3659
write_settings: Callable[[list[dict[str, str]]], Awaitable[None]],
3760
plugin: RtfmPlugin,
@@ -67,6 +90,41 @@ async def set_static_port(request: web.Request):
6790
return web.json_response({"success": True})
6891
return web.json_response({"success": False})
6992

93+
@routes.get("/api/export_settings")
94+
async def export_settings(request: web.Request):
95+
obj = ImportExportSettings(
96+
plugin.static_port,
97+
plugin.main_kw,
98+
[
99+
PartialLibrary(
100+
lib.name,
101+
lib.classname,
102+
None if lib.is_preset else str(lib.loc),
103+
lib.use_cache,
104+
)
105+
for lib in plugin.libraries.values()
106+
],
107+
)
108+
return web.json_response(
109+
{
110+
"success": True,
111+
"data": base64.b64encode(msgspec.json.encode(obj)).decode(),
112+
}
113+
)
114+
115+
@routes.post("/api/import_settings")
116+
async def import_settings(request: web.Request):
117+
raw = await request.content.read()
118+
try:
119+
data = msgspec.json.decode(base64.b64decode(raw), type=ImportExportSettings)
120+
except msgspec.DecodeError:
121+
return web.json_response({"success": False})
122+
123+
await write_settings([lib.to_dict() for lib in data.libraries])
124+
plugin.main_kw = data.keyword
125+
plugin.static_port = data.port
126+
return web.json_response({"success": True})
127+
70128
@routes.get("/")
71129
@aiohttp_jinja2.template("template.html")
72130
async def index(request: web.Request):

plugin/server/script.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,26 @@ async function saveStaticPort() {
325325

326326
console.log("Static Port Saved", resp);
327327
alert("Static Port Updated. Restart flow for it to take affect.");
328+
}
329+
async function importSettings(data) {
330+
let resp = await fetch("/api/import_settings", {
331+
method: "POST",
332+
body: data
333+
}).then(response => response.json())
334+
if (resp['success'] === true){
335+
console.log("Settings Imported", resp);
336+
alert("Settings have been imported.");
337+
} else {
338+
console.log("Data was malformed and settings were not imported successfully.", resp);
339+
alert("Data was malformed and settings were not imported successfully. Restart flow launcher for port setting to take affect.");
340+
}
341+
342+
}
343+
async function exportSettings() {
344+
let resp = await fetch("/api/export_settings", {
345+
method: "GET"
346+
}).then(response => response.json())
347+
navigator.clipboard.writeText(resp['data'])
348+
console.log("Settings exported", resp);
349+
alert("Settings have been successfully expored. You can use the text that has been copied to your clipboard to import them on another device.");
328350
}

plugin/server/template.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
<br>
3636
<span style="font-size: small;">Setting this to 0 will let your system choose the port each time the plugin starts.</span>
3737
</div>
38+
<hr>
39+
<div>
40+
<button class="button" onclick="importSettings(prompt('Code from export: '))">Import Settings</button>
41+
<button class="button" onclick="exportSettings()">Export Settings</button>
42+
</div>
3843
</body>
3944
<script src="data.js"></script>
4045
<script src="script.js"></script>

0 commit comments

Comments
 (0)