HTML text info in screen forms

The 1C:Enterprise developers forum

#1
People who like this:0Yes/0No
Just came
Rating: 1
Joined: May 26, 2012
Company:

Is there a way to store text in HTML format in screen forms?

 
#2
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

HTML is a text format. So you can apply formatting and store it in attributes of String type.

 
#3
People who like this:0Yes/0No
Just came
Rating: 1
Joined: May 26, 2012
Company:

If I copy and paste text in HTML from some web-site and want to store it as it was on that site how to do it? Can you give me some details?

 
#4
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

You need to place an attribute of String type to the form and set Type to HTML document field.
In DocumentCompleted event handler you need to check if the document is loaded: Note to check readyState to be equal to "complete".
HTML Document does not store data in attributes so in order to save data you need to get it from COM object of Document.

In my example I have a catalog with Translation attribute which is not placed on the form. And added Editor form attribute to place it on the form. When the form object is created Editor is filled with HTML formatted text. Note that it must be a valid HTML of version 4 or above. It means that it should at least look like: <html><body>text</body></html>. When the document is loaded the contentEditable is set to "true" (a string) to allow editing. Before the object is written data from the Editor field should be obtained through Document object and assigned to back Translation attribute. And contentEditable should be set to "false" due to when you open it in other HTML document field and it is stored in text it will stay editable which is not required, I believe.

See the script below:

Code
&AtServer
Procedure OnCreateAtServer(Cancel, StandardProcessing)
   Editor = Object.Translation;
EndProcedure

&AtClient
Procedure EditorDocumentComplete(Item)
   If Items.Editor.Document.readyState = "complete" Then
      Items.Editor.Document.body.contentEditable = "true";
   EndIf;
EndProcedure

&AtClient
Procedure BeforeWrite(Cancel, WriteParameters)
   If Items.Editor.Document.readyState = "complete" Then
      Items.Editor.Document.body.contentEditable = "false";
      Object.Translation = Items.Editor.Document.documentElement.outerHTML;
   EndIf;
EndProcedure

 
#5
People who like this:0Yes/0No
Just came
Rating: 1
Joined: May 26, 2012
Company:

How should I load my document? I can't understand what means "if the document is loaded"

 
#6
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

When

Code
Editor = Object.Translation;

is executed the document content is set and it starts internal loading (parsing into DOM and others which a web browser performs on it) procedure when it is ended an event is fired.
So my example is complete enought. Except one thing: if the field is empty insert an empty but valid HTML in there:
Code
If IsBlankString(Object.Translation) Then
    Editor = "<html><body></body></html>";
Else
    Editor = Object.Translation; 
EndIf;

 
#7
People who like this:0Yes/0No
Just came
Rating: 1
Joined: May 26, 2012
Company:

I don't have much experience writing code for 1C v8. Could you please rewrite above code that it would be applicable for a document with attribute Translation. Thanks!

 
#8
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

Code
&AtServer 
Procedure OnCreateAtServer(Cancel, StandardProcessing) 
   If IsBlankString(Object.Translation) Then 
      Editor = "<html><body></body></html>"; 
   Else 
      Editor = Object.Translation; 
   EndIf;
EndProcedure 

&AtClient 
Procedure EditorDocumentComplete(Item) 
   If Items.Editor.Document.readyState = "complete" Then 
      Items.Editor.Document.body.contentEditable = "true"; 
   EndIf; 
EndProcedure 

&AtClient 
Procedure BeforeWrite(Cancel, WriteParameters) 
   If Items.Editor.Document.readyState = "complete" Then 
      Items.Editor.Document.body.contentEditable = "false"; 
      Object.Translation = Items.Editor.Document.documentElement.outerHTML; 
   EndIf; 
EndProcedure

 
#9
People who like this:0Yes/0No
Just came
Rating: 1
Joined: May 26, 2012
Company:

It works. Just when I copy and paste something from the Internet formatting is missing

 
#10
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

You need to study HTML standard to understand how does it work.
In a few words you need to take care both on styles and tags. So to keep formatting you need to copy both of them but you copy only tags. Also note this is a web browser object which handles copy and paste so it happens as it does.
You can provide your own styles for your documents and formatting as you like if you work with source of HTML.

 
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)
Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.