Skip to content

Commit

Permalink
refactor JS config
Browse files Browse the repository at this point in the history
  • Loading branch information
fzumstein committed Nov 11, 2024
1 parent e8eba2d commit a8124ff
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 53 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ repos:
types_or: [html, javascript, css, markdown]
args: [--write]
additional_dependencies:
- prettier@3.1.0
- prettier-plugin-jinja-template@v1.4.1
- prettier@3.3.3
- prettier-plugin-jinja-template@v2.0.0
11 changes: 10 additions & 1 deletion app/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from pathlib import Path
from typing import List, Literal, Optional
from typing import Dict, List, Literal, Optional

import xlwings as xw
from pydantic import UUID4, computed_field
Expand Down Expand Up @@ -57,6 +57,15 @@ class Settings(BaseSettings):
def static_dir(self) -> Path:
return self.base_dir / "static"

@computed_field
@property
def jsconfig(self) -> Dict:
return {
"authProviders": self.auth_providers,
"appPath": self.app_path,
"xlwingsVersion": self.xlwings_version,
}


settings = Settings()

Expand Down
5 changes: 1 addition & 4 deletions app/static/js/auth.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#embedding_data_in_html
const authData = JSON.parse(document.getElementById("auth").text);

globalThis.getAuth = async function () {
if (authData.auth.includes("entraid")) {
if (config.authProviders.includes("entraid")) {
return await xlwings.getAccessToken();
}
return "";
Expand Down
4 changes: 4 additions & 0 deletions app/static/js/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Config
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#embedding_data_in_html
const configElement = document.getElementById("config");
const config = configElement ? JSON.parse(configElement.textContent) : null;
10 changes: 3 additions & 7 deletions app/static/js/core/sheet-buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ Office.onReady(async (info) => {
}
});

async function registerCellToButton(hyperlinkCellRef, scriptName, config) {
async function registerCellToButton(hyperlinkCellRef, scriptName, xwConfig) {
// hyperlinkCellRef is in the form [ShapeName]Sheet1!A1, where [ShapeName] is optional
const appPathElement = document.getElementById("app-path");
const appPath = appPathElement
? JSON.parse(appPathElement.textContent)
: null;
await Excel.run(async (context) => {
let shapeName = null;
let sheetName, cellRef;
Expand Down Expand Up @@ -50,9 +46,9 @@ async function registerCellToButton(hyperlinkCellRef, scriptName, config) {
: "";
await xlwings.runPython(
window.location.origin +
(appPath && appPath.appPath !== "" ? `${appPath.appPath}` : "") +
config.appPath +
`/xlwings/custom-scripts-call/${scriptName}`,
{ ...config, auth: token },
{ ...xwConfig, auth: token },
);
} finally {
sheet.getRange(selectedRangeAddress).getOffsetRange(1, 0).select();
Expand Down
14 changes: 2 additions & 12 deletions app/static/js/core/socketio-handlers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// XLWINGS_APP_PATH setting
const appPathElement = document.getElementById("app-path");
const appPath = appPathElement ? JSON.parse(appPathElement.textContent) : null;

// Socket.io
try {
globalThis.socket = io({
path:
(appPath && appPath.appPath !== "" ? `${appPath.appPath}` : "") +
"/socket.io/",
path: `${config.appPath}/socket.io/`,
auth: async (callback) => {
let token = await globalThis.getAuth();
callback({
Expand All @@ -21,14 +15,10 @@ try {
}

globalThis.socket.on("xlwings:trigger-script", async (data) => {
const appPathElement = document.getElementById("app-path");
const appPath = appPathElement
? JSON.parse(appPathElement.textContent)
: null;
let token = await globalThis.getAuth();
xlwings.runPython(
window.location.origin +
(appPath && appPath.appPath !== "" ? `${appPath.appPath}` : "") +
config.appPath +
"/xlwings/custom-scripts-call/" +
data.script_name,
{ ...data.config, auth: token },
Expand Down
6 changes: 1 addition & 5 deletions app/static/js/core/xlwingsjs/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,13 @@ export async function xlAlert(prompt, title, buttons, mode, callback) {
}

// See xlwings-server repo for how app-path is provided
const appPathElement = document.getElementById("app-path");
const appPath = appPathElement
? JSON.parse(appPathElement.textContent)
: null;
if (dialog) {
dialog.close();
console.log("Closed perviously open dialog to prevent error 12007.");
}
Office.context.ui.displayDialogAsync(
window.location.origin +
(appPath && appPath.appPath !== "" ? `${appPath.appPath}` : "") +
config.appPath +
`/xlwings/alert?prompt=` +
encodeURIComponent(`${prompt}`) +
`&title=` +
Expand Down
23 changes: 11 additions & 12 deletions app/static/js/core/xlwingsjs/xlwings.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,27 @@ export function init() {
typeof globalThis.getAuth === "function"
? await globalThis.getAuth()
: "";
let config = element.getAttribute("xw-config")
let xwConfig = element.getAttribute("xw-config")
? JSON.parse(element.getAttribute("xw-config"))
: {};

await runPython(
const url =
window.location.origin +
(appPath && appPath.appPath !== "" ? `${appPath.appPath}` : "") +
"/xlwings/custom-scripts-call/" +
element.getAttribute("xw-click"),
{ ...config, auth: token, errorDisplayMode: "taskpane" },
);
config.appPath +
"/xlwings/custom-scripts-call/" +
element.getAttribute("xw-click");
await runPython(url, {
...xwConfig,
auth: token,
errorDisplayMode: "taskpane",
});

element.removeChild(spinner);
element.removeAttribute("disabled");
});
});
}

const xlwingsVersion = JSON.parse(
document.getElementById("xlwings-version").text,
);
const version = xlwingsVersion.xlwingsVersion;
const version = config.xlwingsVersion;

globalThis.callbacks = {};
export async function runPython(
Expand Down
16 changes: 6 additions & 10 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Task pane</title>
<link rel="icon" type="image/png" href="{{ url_for('static', path='/images/favicon.png') }}" />
{# Config #}
<script src="{{ url_for('static', path='/js/config.js') }}" defer></script>
{# htmx #}
{% if settings.enable_htmx %}
<script src="{{ url_for('static', path='/vendor/htmx.org/dist/htmx.min.js') }}"></script>
Expand Down Expand Up @@ -38,17 +40,11 @@
<script src="{{ url_for('static', path='/js/core/alpinejs-csp-boilerplate.js') }}"></script>
{% endif %}
{# JS Config #}
<!-- prettier-ignore-start -->
<script id="auth" type="application/json">
{ "auth": {{ settings.auth_providers|tojson }} }
{# prettier-ignore-start #}
<script id="config" type="application/json">
{{ settings.jsconfig|tojson }}
</script>
<script id="app-path" type="application/json">
{ "appPath": {{ settings.app_path|tojson }} }
</script>
<script id="xlwings-version" type="application/json">
{ "xlwingsVersion": {{ settings.xlwings_version|tojson }} }
</script>
<!-- prettier-ignore-end -->
{# prettier-ignore-end #}
<script src="{{ url_for('static', path='/js/auth.js') }}"></script>
{# Load Custom Functions #}
{% block custom_functions_code %}
Expand Down

0 comments on commit a8124ff

Please sign in to comment.