Browse Source

Add missing constructors for TJSBlob and TJSHTMLFile, add a demo for their usage

[email protected] 1 year ago
parent
commit
01ad82b7ee

+ 17 - 0
demo/rtl/demoreadfileinput.html

@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html>
+  <head id="head">
+    <meta charset="utf-8"/>
+    <title>Read file input demo</title>
+    <script type="application/javascript" src="demoreadfileinput.js"></script>
+  </head>
+  <body>
+    <input type="file" id="input">
+    <a id="output" style="visibility:none"></div>
+
+    <script type="application/javascript">
+      rtl.run();
+    </script>
+  </body>
+</html>
+

+ 91 - 0
demo/rtl/demoreadfileinput.lpi

@@ -0,0 +1,91 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<CONFIG>
+  <ProjectOptions>
+    <Version Value="12"/>
+    <General>
+      <Flags>
+        <MainUnitHasCreateFormStatements Value="False"/>
+        <MainUnitHasTitleStatement Value="False"/>
+        <MainUnitHasScaledStatement Value="False"/>
+        <Runnable Value="False"/>
+      </Flags>
+      <SessionStorage Value="InProjectDir"/>
+      <Title Value="demoreadfileinput"/>
+      <UseAppBundle Value="False"/>
+      <ResourceType Value="res"/>
+    </General>
+    <CustomData Count="5">
+      <Item0 Name="MaintainHTML" Value="1"/>
+      <Item1 Name="PasJSHTMLFile" Value="project1.html"/>
+      <Item2 Name="PasJSPort" Value="0"/>
+      <Item3 Name="PasJSWebBrowserProject" Value="1"/>
+      <Item4 Name="RunAtReady" Value="1"/>
+    </CustomData>
+    <BuildModes>
+      <Item Name="Default" Default="True"/>
+    </BuildModes>
+    <PublishOptions>
+      <Version Value="2"/>
+      <UseFileFilters Value="True"/>
+    </PublishOptions>
+    <RunParams>
+      <FormatVersion Value="2"/>
+    </RunParams>
+    <Units>
+      <Unit>
+        <Filename Value="demoreadfileinput.lpr"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="demoreadfileinput.html"/>
+        <IsPartOfProject Value="True"/>
+        <CustomData Count="1">
+          <Item0 Name="PasJSIsProjectHTMLFile" Value="1"/>
+        </CustomData>
+      </Unit>
+    </Units>
+  </ProjectOptions>
+  <CompilerOptions>
+    <Version Value="11"/>
+    <Target FileExt=".js">
+      <Filename Value="demoreadfileinput"/>
+    </Target>
+    <SearchPaths>
+      <IncludeFiles Value="$(ProjOutDir)"/>
+      <UnitOutputDirectory Value="js"/>
+    </SearchPaths>
+    <Parsing>
+      <SyntaxOptions>
+        <AllowLabel Value="False"/>
+        <CPPInline Value="False"/>
+        <UseAnsiStrings Value="False"/>
+      </SyntaxOptions>
+    </Parsing>
+    <CodeGeneration>
+      <TargetOS Value="browser"/>
+    </CodeGeneration>
+    <Linking>
+      <Debugging>
+        <GenerateDebugInfo Value="False"/>
+        <UseLineInfoUnit Value="False"/>
+      </Debugging>
+    </Linking>
+    <Other>
+      <CustomOptions Value="-Jeutf-8 -Jirtl.js -Jc -Jminclude"/>
+      <CompilerPath Value="$(pas2js)"/>
+    </Other>
+  </CompilerOptions>
+  <Debugging>
+    <Exceptions>
+      <Item>
+        <Name Value="EAbort"/>
+      </Item>
+      <Item>
+        <Name Value="ECodetoolError"/>
+      </Item>
+      <Item>
+        <Name Value="EFOpenError"/>
+      </Item>
+    </Exceptions>
+  </Debugging>
+</CONFIG>

+ 42 - 0
demo/rtl/demoreadfileinput.lpr

@@ -0,0 +1,42 @@
+program demoreadfileinput;
+
+uses
+  Web,JS;
+
+const
+  DownloadFileName = 'result.txt';
+var
+  GInput: TJSHTMLInputElement;
+  Goutput: TJSHTMLAnchorElement;
+  LReader: TJSFileReader;
+  LFile: TJSHTMLFile;
+  LFileContent: String;
+begin
+  GInput := TJSHTMLInputElement(Document.GetElementByID('input'));
+  Goutput := TJSHTMLAnchorElement(Document.GetElementByID('output'));
+
+  GInput.OnChange := function (AFileInputChangeEvent: TEventListenerEvent): Boolean
+  begin
+    LFile := GInput.Files[0];
+
+    LReader := TJSFileReader.New;
+    LReader.OnLoad := function (AFileLoadEvent: TEventListenerEvent): Boolean
+    begin
+      LFileContent := String(TJSFileReader(AFileLoadEvent.Target).Result);
+      // begin edit
+      LFileContent := '<pre>' + LineEnding + LFileContent + LineEnding + '</pre>';
+      // end edit
+
+      // need a way to assign back modified LFileContent to LFile or create a new TJS(HTMLFile|Blob) with LFileContent as its content
+      // the Web API standard provides the way as a constructor parameter, but is missing from the declaration
+
+      LFile := TJSHTMLFile.New(TJSString.New(LFileContent), DownloadFileName);
+      Goutput.HRef := TJSURL.createObjectURL(LFile);
+      Goutput.Download := DownloadFileName;
+      Goutput.Click;
+      TJSURL.revokeObjectURL(Goutput.HRef);
+    end;
+    LReader.ReadAsText(LFile);
+  end;
+end.
+

+ 2 - 0
packages/rtl/web.pas

@@ -1825,6 +1825,8 @@ TEventListenerEvent = class external name 'EventListener_Event' (TJSObject)
     FLastModifiedDate: TJSDate; external name 'lastModifiedDate';
     FName: string; external name 'name';
   public
+    constructor New(ABits: JSValue; AName: string); overload;
+    constructor New(ABits: JSValue; AName: string; AOptions: TJSObject); overload;
     property lastModified: NativeInt read FLastModified;
     property lastModifiedDate : TJSDate read FLastModifiedDate; deprecated;
     property name: String read FName;

+ 2 - 0
packages/rtl/weborworker.pas

@@ -244,6 +244,8 @@ type
     FSize: NativeInt; external name 'size';
     FType: string; external name  'type';
   Public
+    constructor New(AArray: JSValue); overload;
+    constructor New(AArray: JSValue; AOptions: TJSObject); overload;
     procedure close;
     function slice : TJSBlob; overload;
     function slice(aStart : NativeInt) : TJSBlob; overload;