Skip to content

Commit

Permalink
fix embedded images
Browse files Browse the repository at this point in the history
fix dark icon
  • Loading branch information
mohzy83 committed May 26, 2022
1 parent 9eaf2e2 commit 3eba8c2
Show file tree
Hide file tree
Showing 19 changed files with 1,935 additions and 3,230 deletions.
55 changes: 29 additions & 26 deletions NppMarkdownPanel/Forms/MarkdownPreviewForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public partial class MarkdownPreviewForm : Form
<html>
";

private Task<string> renderTask;
private Task<Tuple<string, string>> renderTask;
private readonly Action toolWindowCloseAction;
private int lastVerticalScroll = 0;
private string htmlContent;
Expand All @@ -62,6 +62,31 @@ public MarkdownPreviewForm(Action toolWindowCloseAction)
markdownGenerator = MarkdownPanelController.GetMarkdownGeneratorImpl();
}

private Tuple<string,string> RenderHtmlInternal(string currentText, string filepath)
{
var result = markdownGenerator.ConvertToHtml(currentText, filepath);
var resultWithRelativeImages = markdownGenerator.ConvertToHtml(currentText, null);
var defaultBodyStyle = "";

// Path of plugin directory
var markdownStyleContent = "";

var assemblyPath = Path.GetDirectoryName(Assembly.GetAssembly(GetType()).Location);

if (File.Exists(CssFileName))
{
markdownStyleContent = File.ReadAllText(CssFileName);
}
else
{
markdownStyleContent = File.ReadAllText(assemblyPath + "\\" + MainResources.DefaultCssFile);
}

var markdownHtml = string.Format(DEFAULT_HTML_BASE, Path.GetFileName(filepath), markdownStyleContent, defaultBodyStyle, result);
var markdownHtml2 = string.Format(DEFAULT_HTML_BASE, Path.GetFileName(filepath), markdownStyleContent, defaultBodyStyle, resultWithRelativeImages);
return new Tuple<string, string>(markdownHtml, markdownHtml2);
}

public void RenderMarkdown(string currentText, string filepath)
{
if (renderTask == null || renderTask.IsCompleted)
Expand All @@ -70,31 +95,11 @@ public void RenderMarkdown(string currentText, string filepath)
MakeAndDisplayScreenShot();

var context = TaskScheduler.FromCurrentSynchronizationContext();
renderTask = new Task<string>(() =>
{
var result = markdownGenerator.ConvertToHtml(currentText, filepath);
var defaultBodyStyle = "";

// Path of plugin directory
var markdownStyleContent = "";

var assemblyPath = Path.GetDirectoryName(Assembly.GetAssembly(GetType()).Location);

if (File.Exists(CssFileName))
{
markdownStyleContent = File.ReadAllText(CssFileName);
}
else
{
markdownStyleContent = File.ReadAllText(assemblyPath + "\\" + MainResources.DefaultCssFile);
}

var markdownHtml = string.Format(DEFAULT_HTML_BASE, Path.GetFileName(filepath), markdownStyleContent, defaultBodyStyle, result);
return markdownHtml;
});
renderTask = new Task<Tuple<string, string>>(() => RenderHtmlInternal(currentText, filepath));
renderTask.ContinueWith((renderedText) =>
{
htmlContent = renderedText.Result;
webBrowserPreview.DocumentText = renderedText.Result.Item1;
htmlContent = renderedText.Result.Item2;
if (!String.IsNullOrWhiteSpace(HtmlFileName))
{
bool valid = Utils.ValidateFileSelection(HtmlFileName, out string fullPath, out string error, "HTML Output");
Expand All @@ -111,8 +116,6 @@ public void RenderMarkdown(string currentText, string filepath)
}
}
}

webBrowserPreview.DocumentText = htmlContent;
AdjustZoomLevel();
}, context);
renderTask.Start();
Expand Down
13 changes: 11 additions & 2 deletions NppMarkdownPanel/MarkdigMarkdownGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,19 @@ public string ConvertToHtml(string markDownText, string filepath)
pipeline.PreciseSourceLocation = true;
try
{
htmlRenderer.BaseUrl = new Uri(filepath);
if (filepath != null)
{
htmlRenderer.BaseUrl = new Uri(filepath);
}
else
{
htmlRenderer.BaseUrl = null;
}

}
catch
catch (Exception e)
{
if (e != null) {}
}
sb.Clear();

Expand Down
27 changes: 19 additions & 8 deletions NppMarkdownPanel/MarkdownPanelController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public void OnNotification(ScNotification notification)
{
if (notification.Header.Code == (uint)SciMsg.SCN_UPDATEUI)
{
if (syncViewWithCaretPosition && lastCaretPosition != scintillaGateway.GetCurrentPos())
if (syncViewWithCaretPosition && lastCaretPosition != scintillaGateway.GetCurrentPos().Value)
{
lastCaretPosition = scintillaGateway.GetCurrentPos();
lastCaretPosition = scintillaGateway.GetCurrentPos().Value;
ScrollToElementAtLineNo(scintillaGateway.GetCurrentLineNumber());
}
}
Expand Down Expand Up @@ -162,13 +162,24 @@ private void SyncViewWithCaret()

public void SetToolBarIcon()
{
toolbarIcons tbIcons = new toolbarIcons();
tbIcons.hToolbarBmp = Properties.Resources.markdown_16x16_solid.GetHbitmap();
tbIcons.hToolbarIconDarkMode = Properties.Resources.markdown_16x16_solid_dark.GetHbitmap();
IntPtr pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(tbIcons));
Marshal.StructureToPtr(tbIcons, pTbIcons, false);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_ADDTOOLBARICON, PluginBase._funcItems.Items[idMyDlg]._cmdID, pTbIcons);
toolbarIconsOld tbIconsOld = new toolbarIconsOld();
tbIconsOld.hToolbarBmp = Properties.Resources.markdown_16x16_solid.GetHbitmap();
tbIconsOld.hToolbarIcon = Properties.Resources.markdown_16x16_solid_dark.GetHicon();
IntPtr pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(tbIconsOld));
Marshal.StructureToPtr(tbIconsOld, pTbIcons, false);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_ADDTOOLBARICON_DEPRECATED, PluginBase._funcItems.Items[idMyDlg]._cmdID, pTbIcons);
Marshal.FreeHGlobal(pTbIcons);

// if npp version >= 8 then use new NPPM_ADDTOOLBARICON_FORDARKMODE
/* toolbarIconsNew tbIconsNew = new toolbarIconsNew();
tbIconsNew.hToolbarBmp = Properties.Resources.markdown_16x16_solid.GetHbitmap();
tbIconsNew.hToolbarIcon = Properties.Resources.markdown_16x16_solid.GetHicon();
tbIconsNew.hToolbarIconDarkMode = Properties.Resources.markdown_16x16_solid.GetHicon();
// tbIconsOld.hToolbarIconDarkMode = Properties.Resources.markdown_16x16_solid_dark.GetHbitmap();
IntPtr pTbIconsNew = Marshal.AllocHGlobal(Marshal.SizeOf(tbIconsNew));
Marshal.StructureToPtr(tbIconsNew, pTbIconsNew, false);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_ADDTOOLBARICON_FORDARKMODE, PluginBase._funcItems.Items[idMyDlg]._cmdID, pTbIconsNew);
Marshal.FreeHGlobal(pTbIconsNew);*/
}

public void PluginCleanUp()
Expand Down
4 changes: 4 additions & 0 deletions NppMarkdownPanel/NppMarkdownPanel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildProjectDirectory)\PluginInfrastructure\DllExport\NppPlugin.DllExport.targets" />
<PropertyGroup>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
29 changes: 10 additions & 19 deletions NppMarkdownPanel/PluginInfrastructure/ClikeStringArray.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// NPP plugin platform for .Net v0.94.00 by Kasper B. Graversen etc.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Kbg.NppPluginNET.PluginInfrastructure
{
Expand All @@ -20,10 +18,10 @@ public ClikeStringArray(int num, int stringCapacity)
for (int i = 0; i < num; i++)
{
IntPtr item = Marshal.AllocHGlobal(stringCapacity);
Marshal.WriteIntPtr(_nativeArray + (i * IntPtr.Size), item);
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (i * IntPtr.Size)), item);
_nativeItems.Add(item);
}
Marshal.WriteIntPtr(_nativeArray + (num * IntPtr.Size), IntPtr.Zero);
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (num * IntPtr.Size)), IntPtr.Zero);
}
public ClikeStringArray(List<string> lstStrings)
{
Expand All @@ -32,10 +30,10 @@ public ClikeStringArray(List<string> lstStrings)
for (int i = 0; i < lstStrings.Count; i++)
{
IntPtr item = Marshal.StringToHGlobalUni(lstStrings[i]);
Marshal.WriteIntPtr(_nativeArray + (i * IntPtr.Size), item);
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (i * IntPtr.Size)), item);
_nativeItems.Add(item);
}
Marshal.WriteIntPtr(_nativeArray + (lstStrings.Count * IntPtr.Size), IntPtr.Zero);
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (lstStrings.Count * IntPtr.Size)), IntPtr.Zero);
}

public IntPtr NativePointer { get { return _nativeArray; } }
Expand All @@ -54,24 +52,17 @@ List<string> _getManagedItems(bool unicode)

public void Dispose()
{
try
if (!_disposed)
{
if (!_disposed)
{
for (int i = 0; i < _nativeItems.Count; i++)
if (_nativeItems[i] != IntPtr.Zero) Marshal.FreeHGlobal(_nativeItems[i]);
if (_nativeArray != IntPtr.Zero) Marshal.FreeHGlobal(_nativeArray);
_disposed = true;
}
}
catch (Exception e)
{
MessageBox.Show(MethodBase.GetCurrentMethod().ToString() +": "+ e.Message, this.GetType().Name);
for (int i = 0; i < _nativeItems.Count; i++)
if (_nativeItems[i] != IntPtr.Zero) Marshal.FreeHGlobal(_nativeItems[i]);
if (_nativeArray != IntPtr.Zero) Marshal.FreeHGlobal(_nativeArray);
_disposed = true;
}
}
~ClikeStringArray()
{
Dispose();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ public DllExportAttribute(string exportName, CallingConvention callingConvention

public string ExportName { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@
On 32 bit windows usually C:\Program Files\
On 64 bit windows usually C:\Program Files (x86)\
$(ProgramW6432) points to the 64bit Program Files (on 32 bit windows it is blank) -->
<MakeDir Directories="$(MSBuildProgramFiles32)\Notepad++\plugins\$(TargetName)\" Condition="Exists('$(MSBuildProgramFiles32)\Notepad++\plugins\') AND !Exists('$(MSBuildProgramFiles32)\Notepad++\plugins\$(TargetName)\') AND '$(Platform)'=='x86'" />
<Copy
SourceFiles="$(TargetPath)"
DestinationFolder="$(MSBuildProgramFiles32)\Notepad++\plugins\$(TargetName)\"
Condition="Exists('$(MSBuildProgramFiles32)\Notepad++\plugins\$(TargetName)\') AND '$(Platform)'=='x86'"
ContinueOnError="false" />
<MakeDir Directories="$(ProgramW6432)\Notepad++\plugins\$(TargetName)\" Condition="Exists('$(ProgramW6432)\Notepad++\plugins\') AND !Exists('$(ProgramW6432)\Notepad++\plugins\$(TargetName)\') AND '$(Platform)'=='x64'" />
DestinationFolder="$(MSBuildProgramFiles32)\Notepad++\plugins\"
Condition="Exists('$(MSBuildProgramFiles32)\Notepad++\plugins\') AND '$(Platform)'=='x86'"
ContinueOnError="false" />
<Copy
SourceFiles="$(TargetPath)"
DestinationFolder="$(ProgramW6432)\Notepad++\plugins\$(TargetName)\"
Condition="Exists('$(ProgramW6432)\Notepad++\plugins\$(TargetName)\') AND '$(Platform)'=='x64'"
DestinationFolder="$(ProgramW6432)\Notepad++\plugins\"
Condition="Exists('$(ProgramW6432)\Notepad++\plugins\') AND '$(Platform)'=='x64'"
ContinueOnError="false" />
</Target>
</Project>
Loading

0 comments on commit 3eba8c2

Please sign in to comment.