Paste HTML using pBear HtmlImport

General TRichView support forum. Please post your questions here
Marsianin
Posts: 193
Joined: Sun Sep 25, 2005 11:03 pm

Paste HTML using pBear HtmlImport

Post by Marsianin »

Is there a demo? Tried a different methods but doesn't works.
Also I must know is there a HTML in clipboard - did it by

Code: Select all

CF_HTML:=RegisterClipboardFormat('HTML Format');
It seems bBear component is not so flexible to get clipboard content and then export it. Or I'm doing something wrong...
Sergey Tkachenko
Site Admin
Posts: 17312
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

THTMLViewer does not have methods for loading from the Clipboard.
I'll try to explain why it's difficult to implement pasting, if it was not implemented in the component itself.

HTML Clipboard format is a plain text.
It looks like this:

Code: Select all

Version:1.0
StartHTML:000000188
EndHTML:000001603
StartFragment:000001434
EndFragment:000001451
StartSelection:000001434
EndSelection:000001447
SourceURL:http://www.trichview.com/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD><TITLE>Delphi RichEdit Alternative: RichView Components for Delphi and C++Builder</TITLE>
<SCRIPT language=JavaScript type=text/javascript src="scripts.js"></SCRIPT>
<LINK rel=stylesheet type=text/css href="trv.css"><LINK title=RSS rel=alternate 
type=application/rss+xml 
href="http://www.trichview.com/forums/rss.php?f=6"><LINK title=Atom 
rel=alternate type=application/atom+xml 
href="http://www.trichview.com/forums/rss.php?atom&f=6">
<STYLE type=text/css>
	<!--
	.dllist { list-style-image: url('images/icons/download2.gif'); }
	.helplist { list-style-image: url('images/icons/help2.gif'); }
	-->
	</STYLE>
</HEAD>

<BODY onload="MM_preloadAllImages('')" leftMargin=0 topMargin=0 marginheight="0" 
marginwidth="0">

<TABLE border=0 cellSpacing=0 cellPadding=0 width="100%" height="100%">

<TBODY>

<TR>

<TD vAlign=top rowSpan=2 width="100%" align=left>

<TABLE border=0 cellSpacing=5 cellPadding=5 width="100%">

<TBODY>

<TR>

<TD class=main>

<DIV id=trichviewnews class=hslice>

<TABLE border=0 cellSpacing=1 cellPadding=5 width="100%" bgColor=#ffbe0b>

<TBODY>

<TR>

<TD class=news1>

<P><!--StartFragment-->[color=red]<B>September </B>[/color]<!--EndFragment--></P>
</TD>
</TR>
</TBODY>
</TABLE>
</DIV>
</TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
</TBODY>
</TABLE>
</BODY>
</HTML>
(I copied "September " from the main page).
HTML is always represented in UTF-8.

StartHTML..EndHTML define a range of bytes for HTML content.
StartFragment..EndFragment (or StartSelection..EndSelection, I forgot the difference) defines the selected part.

If you extract and load to HTMLViewer a part between StartHTML..EndHTML, this document will contain too many nested tables.
If you extract only selection (marked with red), you lost almost all HTML formatting.
Probably the best compromise is extracting <head> from this HTML, concatenating with the selected part, and loading in THTMLViewer. At least you will have a CSS. But you will lose all context where the selected fragment is inserted.
Marsianin
Posts: 193
Joined: Sun Sep 25, 2005 11:03 pm

Post by Marsianin »

If you extract and load to HTMLViewer a part between StartHTML..EndHTML, this document will contain too many nested tables.
This is a not big problem...

Is there a sample on how to do it?
Sergey Tkachenko
Site Admin
Posts: 17312
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I think these tables makes HTML pasting almost useless. Like in my example, the user copied only one word from the page, but 3 nested tables will be inserted.
Wait, it's an idea. We can remove all 1x1 tables after pasting, I'll make an example.
Sergey Tkachenko
Site Admin
Posts: 17312
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I just uploaded a new version of THTMLViewImporter with new LoadFromClipboard method.
Consider it is as a beta.
It uses some tricks
1) It loads the whole HTML from the Clipboard, then removes unnecessary tables (all 1x1 root tables).
2) It uses THTMLViewer.LoadFromString method. This method has a Reference parameter containing a path to HTML file. Unfortunately, THTMLViewer does not expect URL in this parameter. THTMLViewImporter tricks it, passing 'Z:\' in this parameter, when replacing 'Z:\' to the actual URL in paths to image. At least, it works for <img>.
Marsianin
Posts: 193
Joined: Sun Sep 25, 2005 11:03 pm

Post by Marsianin »

Yeah it works but CLEARS whole document WITHOUT undo.
Here is my code:

Code: Select all

if RVHTMLViewImporter1.LoadFromClipboard(RichViewEdit1) then begin
  RVHTMLViewImporter1.AppendHtmlViewer(HTMLViewer1,RichViewEdit1);
  NormalizeRichView(RichViewEdit1.RVData);
  RichViewEdit1.Format;
  RichViewEdit1.Modified:=True;
end;
So how can I paste from cursor position?
Sergey Tkachenko
Site Admin
Posts: 17312
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Do it how RichViewAction do for TrvHtmlImporter: load in a hidden temporal RichView, save to RVF stream (SaveRVFToStream), insert in the caret position of the main editor (InsertRVFFromStreamEd).
Marsianin
Posts: 193
Joined: Sun Sep 25, 2005 11:03 pm

Post by Marsianin »

There is a problem with images when trying to paste from clipboard.
I did as you advised and everything works fine except images.
I've downloaded image in HTMLViewer1ImageRequest and that image appeared in HTMLViewer1 but this method RVHTMLViewImporter1.LoadFromClipboard(rve) gives me error:
---------------------------
Debugger Exception Notification
---------------------------
Project Project.exe raised exception class EFOpenError with message 'Cannot open file "C:\Users\User\Documents\Delphi\Project\http:\www.google.com\intl\en_ALL\images\logo.gif". The filename, directory name, or volume label syntax is incorrect'.
---------------------------
Break Continue Help
---------------------------
Sergey Tkachenko
Site Admin
Posts: 17312
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Does this url really have http:/ instead of http:// ?
If not, please send me a sample html file, I'll check it when I return
Marsianin
Posts: 193
Joined: Sun Sep 25, 2005 11:03 pm

Post by Marsianin »

I have no idea why it appears as http:\ in the error description but my code loads picture into THTMLViewer and I can see it.
But transferring this HTML page from HTMLViewer to RVE causes this error (the problem is in the RvHtmlViewImporter).
And the source is www.google.com copied from Firefox.
Sergey Tkachenko
Site Admin
Posts: 17312
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Sorry, I cannot reproduce this problem.
When I copy from English version of Google from FireFox, the project tries loading the image from a correct location, i.e. http://www.google.com/intl/en_ALL/images/logo.gif

I use this code:

Code: Select all

  RVHTMLViewImporter1.LoadFromClipboard(RichViewEdit1, HTMLViewer1);
  NormalizeRichView(RichViewEdit1.RVData);
  RichViewEdit1.Format;
Marsianin
Posts: 193
Joined: Sun Sep 25, 2005 11:03 pm

Post by Marsianin »

This code generates such error:

Code: Select all

RVHTMLViewImporter1.LoadFromClipboard(rve);
Calling this function fires

Code: Select all

HTMLViewer1ImageRequest(Sender: TObject; const SRC: string; var Stream: TMemoryStream);
My code handles it and loads image from the SRC and passes it into Stream.
That loaded image APPEARS in HTMLViewer1 after that above error raised.
And pushing continue copies webpage from HTMLViewer1 to RVE without Image.
So I'm getting HTMLViewer1 with loaded image and RVE without.
Sergey Tkachenko
Site Admin
Posts: 17312
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Probably, the code in htmlviewer.OnImageRequest causes this problem, because without it all looks ok.
There is one more reason for not using this event (and using RichView.OnImportPicture instead): relative paths.
Unfortunately, I was not able to find a way for specifying a URL (like 'http://www.google.com/') as a base path. Because of this, when loading from the clipboard, I specify a fake path ('Z:\') and replace it to URL when converting. If you process events of THTMLViewer, you will get paths with Z:\ when pasting. If you process RichView.OnImportPicture, you must get correct paths.
Marsianin
Posts: 193
Joined: Sun Sep 25, 2005 11:03 pm

Post by Marsianin »

But how HTMLViewer.OnImageRequest can cause this problem if it loads image into HTMLViewer itself?
Sergey Tkachenko
Site Admin
Posts: 17312
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I do not know. Please create a test project and send it to me, because I cannot reproduce this problem myself.
Post Reply