Skip to content

Commit

Permalink
测试拖拽方式
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Oct 28, 2021
1 parent 08db2b6 commit 79b90a3
Show file tree
Hide file tree
Showing 17 changed files with 937 additions and 17 deletions.
9 changes: 9 additions & 0 deletions ChromeTest/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="ChromeTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ChromeTest"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
17 changes: 17 additions & 0 deletions ChromeTest/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace ChromeTest
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
10 changes: 10 additions & 0 deletions ChromeTest/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
13 changes: 13 additions & 0 deletions ChromeTest/ChromeTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CefSharp.Wpf.NETCore" Version="94.4.110" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions ChromeTest/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Window x:Class="ChromeTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ChromeTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>

</Grid>
</Window>
28 changes: 28 additions & 0 deletions ChromeTest/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ChromeTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
6 changes: 6 additions & 0 deletions EvernoteToNotionChrome.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EvernoteToNotionChrome", "E
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EvernoteToNotion", "EvernoteToNotion\EvernoteToNotion.csproj", "{B31F147A-02D1-4300-A67F-65C0DB7EC1F2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChromeTest", "ChromeTest\ChromeTest.csproj", "{376D4A9F-6AA8-4A6E-B038-5DC33F7AC6A9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{B31F147A-02D1-4300-A67F-65C0DB7EC1F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B31F147A-02D1-4300-A67F-65C0DB7EC1F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B31F147A-02D1-4300-A67F-65C0DB7EC1F2}.Release|Any CPU.Build.0 = Release|Any CPU
{376D4A9F-6AA8-4A6E-B038-5DC33F7AC6A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{376D4A9F-6AA8-4A6E-B038-5DC33F7AC6A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{376D4A9F-6AA8-4A6E-B038-5DC33F7AC6A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{376D4A9F-6AA8-4A6E-B038-5DC33F7AC6A9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 3 additions & 3 deletions EvernoteToNotionChrome/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ private static void InitializeCefSharp()


// Set BrowserSubProcessPath based on app bitness at runtime
setting.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
Environment.Is64BitProcess ? "x64" : "x86",
"CefSharp.BrowserSubprocess.exe");
//setting.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
// Environment.Is64BitProcess ? "x64" : "x86",
// "CefSharp.BrowserSubprocess.exe");

if (!Cef.Initialize(setting, performDependencyCheck: false, browserProcessHandler: null))
{
Expand Down
4 changes: 1 addition & 3 deletions EvernoteToNotionChrome/EvernoteToNotionChrome.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CefSharp.Wpf">
<Version>86.0.241</Version>
</PackageReference>
<PackageReference Include="CefSharp.Wpf.NETCore" Version="94.4.110" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.36" />
<PackageReference Include="MahApps.Metro" Version="2.4.7" />
<PackageReference Include="Newtonsoft.Json">
Expand Down
4 changes: 3 additions & 1 deletion EvernoteToNotionChrome/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@


<Button Grid.Column="1" x:Name="ButtonStart" Content="Start" Click="ButtonStart_Click" Margin="10,0,0,0" Height="40" Width="100"></Button>

<Button Grid.Column="1" x:Name="ButtonTest" Content="Test" Click="ButtonTest_Click" Margin="10,0,0,0" Height="40" Width="100"></Button>

<StackPanel>
<TextBlock Text="请粘贴Everonte导出的HTML所在目录"></TextBlock>
<TextBox x:Name="TextBoxPath" Text="" Margin="0,5,0,0" Height="26"/>
<TextBlock x:Name="TextBlockStatus" Text="等待开始..." Margin="0,5,0,0" ></TextBlock>

</StackPanel>
</Grid>
</Grid>
Expand Down
15 changes: 15 additions & 0 deletions EvernoteToNotionChrome/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CefSharp;
using CefSharp.Internals;
using EvernoteToNotionChrome.Service;
using EvernoteToNotionChrome.Utils;
using MahApps.Metro.Controls;
Expand All @@ -22,6 +23,9 @@
using System.Windows.Navigation;
using System.Windows.Shapes;




namespace EvernoteToNotionChrome
{
/// <summary>
Expand All @@ -42,6 +46,8 @@ public MainWindow()
browserSettings.JavascriptAccessClipboard = CefSharp.CefState.Enabled;
browserSettings.JavascriptDomPaste = CefSharp.CefState.Enabled;
Browser.BrowserSettings = browserSettings;


}

private void Window_Loaded(object sender, RoutedEventArgs e)
Expand All @@ -52,6 +58,8 @@ private void Window_Loaded(object sender, RoutedEventArgs e)

Browser.RequestHandler = new OxoRequestHandler();
Browser.Address = "https://notion.so/";

var screenInfo = ((IRenderWebBrowser)Browser).GetScreenInfo();

GlobalNotification.Default.Register(GlobalNotification.NotificationOutputLogInfo, this, (msg) => {
Debug.WriteLine(msg.Source);
Expand All @@ -74,6 +82,13 @@ await Task.Run(() => {
ButtonStart.IsEnabled = true;
}

private async void ButtonTest_Click(object sender, RoutedEventArgs e)
{
await UploadManager.UploadFile(@"C:\Users\aiqin\Documents\Tencent Files\76835052\FileRecv\tinified (5).zip");
}



private void Browser_Loaded(object sender, RoutedEventArgs e)
{

Expand Down
59 changes: 49 additions & 10 deletions EvernoteToNotionChrome/Service/HtmlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace EvernoteToNotionChrome.Service
{
public class HtmlManager
Expand Down Expand Up @@ -35,6 +37,9 @@ public static void UploadHtmlData(string filePath)

List<string> imageList = new List<string>();

List<string> fileList = new List<string>();

//处理img标签
var nodes = doc.DocumentNode.SelectNodes("//img");

if (nodes == null)
Expand All @@ -55,19 +60,57 @@ public static void UploadHtmlData(string filePath)

GlobalNotification.Default.Post(GlobalNotification.NotificationOutputLogInfo, $"处理IMAGE标签:{src.Value}" );

if (!src.Value.Contains("en_todo"))
if (link.ParentNode.Name == "a")
{
Debug.WriteLine("上级node是a标签,文件类型,不按照图片处理");
HtmlNode parentNode = link.ParentNode;
string href = parentNode.Attributes["href"].Value; //文件路径
fileList.Add(href);
}
else if (!src.Value.Contains("en_todo"))
{
//读取图片尺寸并替换到image标签,让notion识别宽高
var fullFilePath = path + @"\" + src.Value.Replace(@"/", @"\");
Size imageSize = GetImageSize(fullFilePath);
link.SetAttributeValue("style", $"width: {imageSize.Width}px; height: {imageSize.Height}px;");

//如果上级是a标签,则不处理,后续按照a标签处理
imageList.Add(src.Value);
}
else
{
ConversionTodoList(link, path, src.Value);

}
}

//读取图片,上传
var docString = doc.DocumentNode.OuterHtml;
//上传所有图片并替换到html文件中
var docString = UploadAllImage(imageList, path, doc.DocumentNode.OuterHtml);

docString = UploadAllImage(fileList, path, docString);


Debug.WriteLine(doc.DocumentNode.OuterHtml);

File.WriteAllText(savePath, doc.DocumentNode.OuterHtml);

}

static Size GetImageSize(string path)
{
Image image = Bitmap.FromFile(path);
return image.Size;
}


/// <summary>
/// 上传
/// </summary>
/// <param name="localImagePaths"></param>
/// <param name=""></param>
/// <returns></returns>
private static string UploadAllImage(List<string> imageList, string path, string doc)
{
var docString = doc;
foreach (string file in imageList)
{
if (file.StartsWith("file:") || file.StartsWith("http:") || file.StartsWith("https:"))
Expand All @@ -93,16 +136,12 @@ public static void UploadHtmlData(string filePath)
}

}

Debug.WriteLine(docString);

File.WriteAllText(savePath, docString);

return docString;
}



static void ConversionTodoList(HtmlNode node, string basePath, string path)
private static void ConversionTodoList(HtmlNode node, string basePath, string path)
{

var parentNode = node.ParentNode;
Expand Down
26 changes: 26 additions & 0 deletions EvernoteToNotionChrome/Service/UploadManager.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
using CefSharp;
using CefSharp.Internals;
using EvernoteToNotionChrome.Utils;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace EvernoteToNotionChrome.Service
{

public class UploadManager
{
public static bool IsUploading { set; get; }
Expand All @@ -31,8 +37,28 @@ public static async Task<string> UploadFile(string filePath)
Clipboard.SetImage(bSource);
MainWindow.SingleInstance.Browser.Paste();
}
catch (NotSupportedException ex)
{
//filePath不是图片
Debug.WriteLine(ex);
StringCollection list = new ();
list.Add(filePath);
Debug.WriteLine("设置文件到剪贴板");
//Clipboard.SetFileDropList(list);
//MainWindow.SingleInstance.Browser.Paste();
//Core.DragData.Create()
//IDragData dragData = DragData.Create();
//dragData.FileName = filePath;
//dragData.IsFile = true;

//MainWindow.SingleInstance.Browser.GetBrowserHost().GetWindowHandle()
Win32Drap.SendFileDrop(new WindowInteropHelper(MainWindow.SingleInstance).Handle, filePath, 550, 400);

//((IRenderWebBrowser)MainWindow.SingleInstance.Browser).StartDragging(dragData, CefSharp.Enums.DragOperationsMask.Move, 550, 400);
}
catch (System.Exception ex)
{
Debug.WriteLine(ex);
hasError = true;
}
});
Expand Down
Loading

0 comments on commit 79b90a3

Please sign in to comment.