Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reblocks.asd
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"reblocks/debug"
"reblocks/default-init"
"reblocks/commands-hook"
"reblocks/commands-history"
"reblocks/widgets/string-widget"
"reblocks/widgets/funcall-widget"
"reblocks/welcome/widget"
Expand Down
40 changes: 40 additions & 0 deletions src/commands-history.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(defpackage #:reblocks/commands/history
(:use #:cl)
(:import-from #:reblocks/actions
#:make-action)
(:import-from #:reblocks/commands
#:add-command)
(:export #:browser-history-push-state
#:browser-history-back
#:browser-history-forward))

(in-package :reblocks/commands/history)

(defun browser-history-push-state (url &key (state "{}")
pop-action)
"Invoke history.pushState on the client.

See: https://developer.mozilla.org/en-US/docs/Web/API/History_API"
(let ((action-code (when pop-action
(etypecase pop-action
(string pop-action)
(function (reblocks/actions:make-action pop-action))))))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the docstring to browser-history-push-state I think we should mention that an action will be called with two arguments location and state.

Also, I'd prefer state argument of the browser-history-push-state to be a hash-table, but not sure if it will be deserialized into a hash-table propertly when action us called by the frontend.

(add-command :update-history
:operation "pushState"
:state state
:url url
:pop-action-code action-code)))

(defun browser-history-forward ()
"Invoke history.forward() on the client.

See: https://developer.mozilla.org/en-US/docs/Web/API/History_API"
(add-command :update-history
:operation "forward"))

(defun browser-history-back ()
"Invoke history.back() on the client.

See: https://developer.mozilla.org/en-US/docs/Web/API/History_API"
(add-command :update-history
:operation "back"))
2 changes: 1 addition & 1 deletion src/commands.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#:get-collected-commands
#:with-collected-commands
#:add-commands))
(in-package #:reblocks/commands)

(in-package #:reblocks/commands)

(defvar *commands*)

Expand Down
31 changes: 31 additions & 0 deletions src/js/jquery/jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,39 @@ window.commandHandlers = {
'includeJS': function(params) {
include_dom(params.url);
},
'updateHistory' : function(params) {
switch(params.operation) {
case 'pushState':
let historyState = {
//location: document.location,
state: JSON.parse(params.state),
popActionCode: params.popActionCode
};
history.pushState(historyState, '', params.url);
break;
case 'back':
history.back();
break;
case 'forward':
history.forward();
break;
}
}
};

window.addEventListener("popstate", (event) => {
let historyState = event.state;
console.log(historyState);
if (historyState?.popActionCode) {
initiateAction(historyState.popActionCode, {
args: {
location: document.location,
state: JSON.stringify(historyState.state)
}
});
}
});

function processCommand(command) {
var method = command.method;
// TODO: add a documentation on defining a command handler
Expand Down
Loading