From 1e920510ea0869bf1421a1b97916ed550d445e24 Mon Sep 17 00:00:00 2001 From: David Botton Date: Tue, 16 Feb 2021 10:13:40 -0500 Subject: [PATCH] Real clipboard use and navigation prevention --- clog-window.lisp | 5 +++-- demos/03-demo.lisp | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/clog-window.lisp b/clog-window.lisp index 8993a11..77a01b4 100644 --- a/clog-window.lisp +++ b/clog-window.lisp @@ -423,8 +423,9 @@ is nil unbind the event.")) ;;;;;;;;;;;;;;;;;;;;;;;;;; (defgeneric set-on-before-unload (clog-window on-before-unload-handler) - (:documentation "Set the ON-BEFORE-UNLOAD-HANDLER for CLOG-OBJ. If -ON-BEFORE-UNLOAD-HANDLER is nil unbind the event.")) + (:documentation "Set the ON-BEFORE-UNLOAD-HANDLER for CLOG-WINDOW. +Return and empty string in order to prevent navigation off page. +If ON-BEFORE-UNLOAD-HANDLER is nil unbind the event.")) (defmethod set-on-before-unload ((obj clog-window) handler) (set-event obj "beforeunload" diff --git a/demos/03-demo.lisp b/demos/03-demo.lisp index d037d57..ff7ce3f 100644 --- a/demos/03-demo.lisp +++ b/demos/03-demo.lisp @@ -154,12 +154,29 @@ (sleep 2) (setf (window-title cw) fname)))) +(defun do-ide-edit-undo (obj) + (let ((cw (current-window obj))) + (when cw + (do-ide-edit-copy obj) + (js-execute obj (format nil "editor_~A.execCommand('undo')" + (html-id cw)))))) + +(defun do-ide-edit-redo (obj) + (let ((cw (current-window obj))) + (when cw + (do-ide-edit-copy obj) + (js-execute obj (format nil "editor_~A.execCommand('redo')" + (html-id cw)))))) + (defun do-ide-edit-copy (obj) (let ((cw (current-window obj))) (when cw (let* ((app (connection-data-item obj "app-data"))) - (setf (copy-buf app) (js-query obj (format nil "editor_~A.getCopyText();" - (html-id cw)))))))) + (setf (copy-buf app) (js-query obj + (format nil "editor_~A.execCommand('copy');~ + navigator.clipboard.writeText(editor_~A.getCopyText());~ + editor_~A.getCopyText();" + (html-id cw) (html-id cw) (html-id cw)))))))) (defun do-ide-edit-cut (obj) (let ((cw (current-window obj))) @@ -172,9 +189,12 @@ (let ((cw (current-window obj))) (when cw (let ((app (connection-data-item obj "app-data"))) - (js-execute obj (format nil "editor_~A.execCommand('paste', '~A')" - (html-id cw) - (escape-string (copy-buf app)))))))) + ;; Note this methods uses the global clip buffer and not (copy-buf app) + ;; on copy and paste we set both the global and local buffer. + (js-execute obj (format nil "navigator.clipboard.readText().then(function(text) {~ + editor_~A.execCommand('paste', text)~ + })" + (html-id cw))))))) (defun do-ide-lisp-eval-file (obj) (let ((cw (current-window obj))) @@ -225,6 +245,8 @@ (create-gui-menu-item file :content "Open" :on-click #'do-ide-file-open) (create-gui-menu-item file :content "Save" :on-click #'do-ide-file-save) (create-gui-menu-item file :content "Save As" :on-click #'do-ide-file-save-as) + (create-gui-menu-item edit :content "Undo" :on-click #'do-ide-edit-undo) + (create-gui-menu-item edit :content "Redo" :on-click #'do-ide-edit-redo) (create-gui-menu-item edit :content "Copy" :on-click #'do-ide-edit-copy) (create-gui-menu-item edit :content "Cut" :on-click #'do-ide-edit-cut) (create-gui-menu-item edit :content "Paste" :on-click #'do-ide-edit-paste) @@ -234,6 +256,10 @@ (create-gui-menu-window-select wind) (create-gui-menu-item help :content "About" :on-click #'do-ide-help-about) (create-gui-menu-full-screen menu)) + + (set-on-before-unload (window body) (lambda(obj) + ;; return empty string to prevent nav off page + "")) (run body)) (defun start-demo ()