Skip to content

Commit

Permalink
feat(library): keydown events binding and copy/paste for clipboard ma…
Browse files Browse the repository at this point in the history
…nagement
  • Loading branch information
Enzo707 committed Oct 17, 2023
1 parent 10cfb3a commit 7f12194
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
72 changes: 69 additions & 3 deletions src/LibraryViewExtensionWebView2/LibraryViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ namespace Dynamo.LibraryViewExtensionWebView2
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisibleAttribute(true)]

public class ScriptObject
{
Action<string> onCopyToClipboard;
Action onPasteFromClipboard;

internal ScriptObject(Action<string> onCopyToClipboard, Action onPasteFromClipboard)
{
this.onCopyToClipboard = onCopyToClipboard;
this.onPasteFromClipboard = onPasteFromClipboard;
}
public void CopyToClipboard(string text)
{
onCopyToClipboard(text);
}
public string PasteFromClipboard()
{
var text = Clipboard.GetText();
return text;
}

}

public class LibraryViewController : IDisposable
{
private Window dynamoWindow;
Expand Down Expand Up @@ -193,6 +216,13 @@ internal void RefreshLibraryView(WebView2 browser)

#endregion

internal void OnCopyToClipboard(string text)
{
Clipboard.SetText(text);
}

internal void OnPasteFromClipboard() { }

private string ReplaceUrlWithBase64Image(string html, string minifiedURL, bool magicreplace = true)
{
var ext = string.Empty;
Expand Down Expand Up @@ -349,6 +379,10 @@ private void Browser_CoreWebView2InitializationCompleted(object sender, CoreWebV
browser.ZoomFactor = (double)dynamoViewModel.Model.PreferenceSettings.LibraryZoomScale / 100;
browser.ZoomFactorChanged += Browser_ZoomFactorChanged;
browser.KeyDown += Browser_KeyDown;

// Hosts an object that will expose the properties and methods to be called from the javascript side
browser.CoreWebView2.AddHostObjectToScript("scriptObject",
new ScriptObject(OnCopyToClipboard, OnPasteFromClipboard));
}

private void Browser_Loaded(object sender, RoutedEventArgs e)
Expand All @@ -357,13 +391,45 @@ private void Browser_Loaded(object sender, RoutedEventArgs e)
LogToDynamoConsole(msg);
}

/// <summary>
/// Collect the main and modifier key from KeyEventArgs in order to pass
/// that data to eventDispatcher (located in library.html) which is responsible
/// for binding KeyDown events between dynamo and webview instances
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///

// This enum is for matching the modifier keys between C# and javaScript
enum ModifiersJS
{
none = 0,
altKey = 1,
ctrlKey = 2,
shiftKey = 4
}

// This enum is for define the events to be tracked
enum EventsTracked
{
Delete,
C,
V
}

private void Browser_KeyDown(object sender, KeyEventArgs e)

{
if (e.Key == Key.Delete)

if (!Enum.IsDefined(typeof(EventsTracked), e.Key.ToString())) return;

var synteticEventData = new Dictionary<string, string>
{
_ = ExecuteScriptFunctionAsync(browser, "eventDispatcher");
}
[Enum.GetName(typeof(ModifiersJS), e.KeyboardDevice.Modifiers)] = "true",
["key"] = e.Key.ToString()
};

_ = ExecuteScriptFunctionAsync(browser, "eventDispatcher", synteticEventData);
}


Expand Down

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/LibraryViewExtensionWebView2/web/library/library.html
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,13 @@
window.chrome.webview.postMessage(JSON.stringify({ "func": "ResizedEvent", "data": "" }));
}

function eventDispatcher() {

//This function will be dispatching javaScript keydown events based on Dynamo keydown events
function eventDispatcher(eventKeyData) {
const kbEvent = new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
key: 'Delete',
});
...eventKeyData
});

document.dispatchEvent(kbEvent);
}
Expand Down

0 comments on commit 7f12194

Please sign in to comment.