Implement text selection in wxWebViewEdge

Text selection/copy/paste etc implemented via javascript
This commit is contained in:
Tobias Taschner
2020-01-16 20:50:26 +01:00
parent 048c1c4321
commit bb508dc347
3 changed files with 36 additions and 23 deletions

View File

@@ -111,6 +111,9 @@ public:
virtual void EnableAccessToDevTools(bool enable = true) wxOVERRIDE; virtual void EnableAccessToDevTools(bool enable = true) wxOVERRIDE;
virtual bool IsAccessToDevToolsEnabled() const wxOVERRIDE; virtual bool IsAccessToDevToolsEnabled() const wxOVERRIDE;
bool QueryCommandEnabled(const wxString& command) const;
void ExecCommand(const wxString& command);
virtual bool RunScript(const wxString& javascript, wxString* output = NULL) wxOVERRIDE; virtual bool RunScript(const wxString& javascript, wxString* output = NULL) wxOVERRIDE;
virtual void RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) wxOVERRIDE; virtual void RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) wxOVERRIDE;

View File

@@ -285,7 +285,7 @@ public:
<a href="https://docs.microsoft.com/en-us/microsoft-edge/hosting/webview2">Edge WebView2</a>. <a href="https://docs.microsoft.com/en-us/microsoft-edge/hosting/webview2">Edge WebView2</a>.
It is available for Windows 7 and newer. It is available for Windows 7 and newer.
The following features are currently unsupported with this backend: The following features are currently unsupported with this backend:
virtual filesystems, custom urls, text selection, find, source code. virtual filesystems, custom urls, find, source code.
This backend is not enabled by default, to build it follow these steps: This backend is not enabled by default, to build it follow these steps:
- Visual Studio 2015, or newer, is required - Visual Studio 2015, or newer, is required

View File

@@ -598,57 +598,52 @@ void wxWebViewEdge::SetZoom(wxWebViewZoom zoom)
bool wxWebViewEdge::CanCut() const bool wxWebViewEdge::CanCut() const
{ {
// TODO: not implemented in SDK (could probably be implemented by script) return QueryCommandEnabled("cut");
return false;
} }
bool wxWebViewEdge::CanCopy() const bool wxWebViewEdge::CanCopy() const
{ {
// TODO: not implemented in SDK (could probably be implemented by script) return QueryCommandEnabled("copy");
return false;
} }
bool wxWebViewEdge::CanPaste() const bool wxWebViewEdge::CanPaste() const
{ {
// TODO: not implemented in SDK (could probably be implemented by script) return QueryCommandEnabled("paste");
return false;
} }
void wxWebViewEdge::Cut() void wxWebViewEdge::Cut()
{ {
// TODO: not implemented in SDK (could probably be implemented by script) ExecCommand("cut");
} }
void wxWebViewEdge::Copy() void wxWebViewEdge::Copy()
{ {
// TODO: not implemented in SDK (could probably be implemented by script) ExecCommand("copy");
} }
void wxWebViewEdge::Paste() void wxWebViewEdge::Paste()
{ {
// TODO: not implemented in SDK (could probably be implemented by script) ExecCommand("paste");
} }
bool wxWebViewEdge::CanUndo() const bool wxWebViewEdge::CanUndo() const
{ {
// TODO: not implemented in SDK (could probably be implemented by script) return QueryCommandEnabled("undo");
return false;
} }
bool wxWebViewEdge::CanRedo() const bool wxWebViewEdge::CanRedo() const
{ {
// TODO: not implemented in SDK (could probably be implemented by script) return QueryCommandEnabled("redo");
return false;
} }
void wxWebViewEdge::Undo() void wxWebViewEdge::Undo()
{ {
// TODO: not implemented in SDK (could probably be implemented by script) ExecCommand("undo");
} }
void wxWebViewEdge::Redo() void wxWebViewEdge::Redo()
{ {
// TODO: not implemented in SDK (could probably be implemented by script) ExecCommand("redo");
} }
long wxWebViewEdge::Find(const wxString& WXUNUSED(text), int WXUNUSED(flags)) long wxWebViewEdge::Find(const wxString& WXUNUSED(text), int WXUNUSED(flags))
@@ -670,24 +665,26 @@ bool wxWebViewEdge::IsEditable() const
void wxWebViewEdge::SelectAll() void wxWebViewEdge::SelectAll()
{ {
// TODO: not implemented in SDK (could probably be implemented by script) RunScript("window.getSelection().selectAllChildren(document);");
} }
bool wxWebViewEdge::HasSelection() const bool wxWebViewEdge::HasSelection() const
{ {
// TODO: not implemented in SDK (could probably be implemented by script) wxString rangeCountStr;
return false; const_cast<wxWebViewEdge*>(this)->RunScript("window.getSelection().rangeCount;", &rangeCountStr);
return rangeCountStr != "0";
} }
void wxWebViewEdge::DeleteSelection() void wxWebViewEdge::DeleteSelection()
{ {
// TODO: not implemented in SDK (could probably be implemented by script) ExecCommand("delete");
} }
wxString wxWebViewEdge::GetSelectedText() const wxString wxWebViewEdge::GetSelectedText() const
{ {
// TODO: not implemented in SDK (could probably be implemented by script) wxString selectedText;
return wxString(); const_cast<wxWebViewEdge*>(this)->RunScript("window.getSelection().toString();", &selectedText);
return selectedText;
} }
wxString wxWebViewEdge::GetSelectedSource() const wxString wxWebViewEdge::GetSelectedSource() const
@@ -698,7 +695,7 @@ wxString wxWebViewEdge::GetSelectedSource() const
void wxWebViewEdge::ClearSelection() void wxWebViewEdge::ClearSelection()
{ {
// TODO: not implemented in SDK (could probably be implemented by script) RunScript("window.getSelection().empty();");
} }
void wxWebViewEdge::EnableContextMenu(bool enable) void wxWebViewEdge::EnableContextMenu(bool enable)
@@ -749,6 +746,19 @@ void* wxWebViewEdge::GetNativeBackend() const
return m_impl->m_webView; return m_impl->m_webView;
} }
bool wxWebViewEdge::QueryCommandEnabled(const wxString& command) const
{
wxString resultStr;
const_cast<wxWebViewEdge*>(this)->RunScript(
wxString::Format("function f(){ return document.queryCommandEnabled('%s'); } f();", command), &resultStr);
return resultStr.IsSameAs("true", false);
}
void wxWebViewEdge::ExecCommand(const wxString& command)
{
RunScript(wxString::Format("document.execCommand('%s');", command));
}
bool wxWebViewEdge::RunScriptSync(const wxString& javascript, wxString* output) bool wxWebViewEdge::RunScriptSync(const wxString& javascript, wxString* output)
{ {
bool scriptExecuted = false; bool scriptExecuted = false;