Skip to content

Commit

Permalink
chore: remove hotkey.js script and replace var with const (#7343)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog authored Jul 12, 2023
1 parent a2be88a commit 71a0240
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 72 deletions.
10 changes: 5 additions & 5 deletions core/tauri/scripts/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
callback,
once
) {
var identifier = uid()
var prop = `_${identifier}`
const identifier = uid()
const prop = `_${identifier}`

Object.defineProperty(window, prop, {
value: (result) => {
Expand Down Expand Up @@ -50,11 +50,11 @@

window.__TAURI_INVOKE__ = function invoke(cmd, args = {}) {
return new Promise(function (resolve, reject) {
var callback = window.__TAURI__.transformCallback(function (r) {
const callback = window.__TAURI__.transformCallback(function (r) {
resolve(r)
delete window[`_${error}`]
}, true)
var error = window.__TAURI__.transformCallback(function (e) {
const error = window.__TAURI__.transformCallback(function (e) {
reject(e)
delete window[`_${callback}`]
}, true)
Expand Down Expand Up @@ -211,7 +211,7 @@
}

window.Notification = function (title, options) {
var opts = options || {}
const opts = options || {}
sendNotification(
Object.assign(opts, {
title: title
Expand Down
6 changes: 0 additions & 6 deletions core/tauri/scripts/hotkey.js

This file was deleted.

36 changes: 36 additions & 0 deletions core/tauri/scripts/toggle-devtools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

(function () {
function toggleDevtoolsHotkey() {
const isHotkey = navigator.appVersion.includes("Mac")
? (event) => event.metaKey && event.altKey && event.key === "I"
: (event) => event.ctrlKey && event.shiftKey && event.key === "I";

document.addEventListener("keydown", (event) => {
if (isHotkey(event)) {
window.__TAURI_INVOKE__('tauri', {
__tauriModule: 'Window',
message: {
cmd: 'manage',
data: {
cmd: {
type: '__toggleDevtools'
}
}
}
});
}
});
}

if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
toggleDevtoolsHotkey();
} else {
window.addEventListener("DOMContentLoaded", toggleDevtoolsHotkey, true);
}
})();
25 changes: 1 addition & 24 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,30 +778,7 @@ impl<R: Runtime> WindowManager<R> {
};

#[cfg(any(debug_assertions, feature = "devtools"))]
let hotkeys = &format!(
"
{};
window.hotkeys('{}', () => {{
window.__TAURI_INVOKE__('tauri', {{
__tauriModule: 'Window',
message: {{
cmd: 'manage',
data: {{
cmd: {{
type: '__toggleDevtools'
}}
}}
}}
}});
}});
",
include_str!("../scripts/hotkey.js"),
if cfg!(target_os = "macos") {
"command+option+i"
} else {
"ctrl+shift+i"
}
);
let hotkeys = include_str!("../scripts/toggle-devtools.js");
#[cfg(not(any(debug_assertions, feature = "devtools")))]
let hotkeys = "";

Expand Down
12 changes: 6 additions & 6 deletions examples/api/src/views/Dialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
let directory = false
function arrayBufferToBase64(buffer, callback) {
var blob = new Blob([buffer], {
const blob = new Blob([buffer], {
type: 'application/octet-binary'
})
var reader = new FileReader()
const reader = new FileReader()
reader.onload = function (evt) {
var dataurl = evt.target.result
const dataurl = evt.target.result
callback(dataurl.substr(dataurl.indexOf(',') + 1))
}
reader.readAsDataURL(blob)
Expand All @@ -40,8 +40,8 @@
if (Array.isArray(res)) {
onMessage(res)
} else {
var pathToRead = res
var isFile = pathToRead.match(/\S+\.\S+$/g)
const pathToRead = res
const isFile = pathToRead.match(/\S+\.\S+$/g)
readBinaryFile(pathToRead)
.then(function (response) {
if (isFile) {
Expand All @@ -52,7 +52,7 @@
arrayBufferToBase64(
new Uint8Array(response),
function (base64) {
var src = 'data:image/png;base64,' + base64
const src = 'data:image/png;base64,' + base64
insecureRenderHtml('<img src="' + src + '"></img>')
}
)
Expand Down
28 changes: 14 additions & 14 deletions examples/multiwindow/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
<div id="response"></div>

<script>
var WebviewWindow = window.__TAURI__.window.WebviewWindow
var appWindow = window.__TAURI__.window.appWindow
var windowLabel = appWindow.label
var windowLabelContainer = document.getElementById('window-label')
const WebviewWindow = window.__TAURI__.window.WebviewWindow
const appWindow = window.__TAURI__.window.appWindow
const windowLabel = appWindow.label
const windowLabelContainer = document.getElementById('window-label')
windowLabelContainer.innerText = 'This is the ' + windowLabel + ' window.'

var container = document.getElementById('container')
const container = document.getElementById('container')

function createWindowMessageBtn(label) {
var tauriWindow = WebviewWindow.getByLabel(label)
var button = document.createElement('button')
const tauriWindow = WebviewWindow.getByLabel(label)
const button = document.createElement('button')
button.innerText = 'Send message to ' + label
button.addEventListener('click', function () {
tauriWindow.emit('clicked', 'message from ' + windowLabel)
Expand All @@ -33,6 +33,7 @@
}

// global listener
const responseContainer = document.getElementById('response')
window.__TAURI__.event.listen('clicked', function (event) {
responseContainer.innerHTML +=
'Got ' + JSON.stringify(event) + ' on global listener\n\n'
Expand All @@ -41,17 +42,16 @@
createWindowMessageBtn(event.payload.label)
})

var responseContainer = document.getElementById('response')
// listener tied to this window
appWindow.listen('clicked', function (event) {
responseContainer.innerText +=
'Got ' + JSON.stringify(event) + ' on window listener\n\n'
})

var createWindowButton = document.createElement('button')
const createWindowButton = document.createElement('button')
createWindowButton.innerHTML = 'Create window'
createWindowButton.addEventListener('click', function () {
var webviewWindow = new WebviewWindow(
const webviewWindow = new WebviewWindow(
Math.random().toString().replace('.', ''),
{
tabbingIdentifier: windowLabel
Expand All @@ -66,17 +66,17 @@
})
container.appendChild(createWindowButton)

var globalMessageButton = document.createElement('button')
const globalMessageButton = document.createElement('button')
globalMessageButton.innerHTML = 'Send global message'
globalMessageButton.addEventListener('click', function () {
// emit to all windows
window.__TAURI__.event.emit('clicked', 'message from ' + windowLabel)
})
container.appendChild(globalMessageButton)

var allWindows = window.__TAURI__.window.getAll()
for (var index in allWindows) {
var label = allWindows[index].label
const allWindows = window.__TAURI__.window.getAll()
for (const index in allWindows) {
const label = allWindows[index].label
if (label === windowLabel) {
continue
}
Expand Down
18 changes: 9 additions & 9 deletions examples/parent-window/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
<div id="response"></div>

<script>
var WebviewWindow = window.__TAURI__.window.WebviewWindow
var thisTauriWindow = window.__TAURI__.window.getCurrent()
var windowLabel = thisTauriWindow.label
var windowLabelContainer = document.getElementById('window-label')
const WebviewWindow = window.__TAURI__.window.WebviewWindow
const thisTauriWindow = window.__TAURI__.window.getCurrent()
const windowLabel = thisTauriWindow.label
const windowLabelContainer = document.getElementById('window-label')
windowLabelContainer.innerText = 'This is the ' + windowLabel + ' window.'

var container = document.getElementById('container')
const container = document.getElementById('container')

var responseContainer = document.getElementById('response')
const responseContainer = document.getElementById('response')
function runCommand(commandName, args, optional) {
window.__TAURI__
.invoke(commandName, args)
Expand All @@ -37,9 +37,9 @@
responseContainer.innerText += 'Got window-created event\n\n'
})

var createWindowButton = document.createElement('button')
var windowId = Math.random().toString().replace('.', '')
var windowNumber = 1
const createWindowButton = document.createElement('button')
const windowId = Math.random().toString().replace('.', '')
const windowNumber = 1
createWindowButton.innerHTML = 'Create child window ' + windowNumber
createWindowButton.addEventListener('click', function () {
runCommand('create_child_window', {
Expand Down
6 changes: 3 additions & 3 deletions tooling/cli/node/test/jest/fixtures/app/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
}

function testFs(dir) {
var contents = 'TAURI E2E TEST FILE'
var commandSuffix = dir ? 'WithDir' : ''
const contents = 'TAURI E2E TEST FILE'
const commandSuffix = dir ? 'WithDir' : ''

var options = {
const options = {
dir: dir || null
}

Expand Down
10 changes: 5 additions & 5 deletions tooling/cli/src/helpers/auto-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
// taken from https://github.com/thedodd/trunk/blob/5c799dc35f1f1d8f8d3d30c8723cbb761a9b6a08/src/autoreload.js

;(function () {
var url = 'ws:' + '//' + window.location.host + '/_tauri-cli/ws'
var poll_interval = 5000
var reload_upon_connect = () => {
const url = 'ws:' + '//' + window.location.host + '/_tauri-cli/ws'
const poll_interval = 5000
const reload_upon_connect = () => {
window.setTimeout(() => {
// when we successfully reconnect, we'll force a
// reload (since we presumably lost connection to
// trunk due to it being killed, so it will have
// rebuilt on restart)
var ws = new WebSocket(url)
const ws = new WebSocket(url)
ws.onopen = () => window.location.reload()
ws.onclose = reload_upon_connect
}, poll_interval)
}

var ws = new WebSocket(url)
const ws = new WebSocket(url)
ws.onmessage = (ev) => {
const msg = JSON.parse(ev.data)
if (msg.reload) {
Expand Down

0 comments on commit 71a0240

Please sign in to comment.