aboutsummaryrefslogtreecommitdiff
path: root/src/client/documents/DocUtils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/documents/DocUtils.ts')
-rw-r--r--src/client/documents/DocUtils.ts57
1 files changed, 56 insertions, 1 deletions
diff --git a/src/client/documents/DocUtils.ts b/src/client/documents/DocUtils.ts
index 18c8d97d4..7f22e9376 100644
--- a/src/client/documents/DocUtils.ts
+++ b/src/client/documents/DocUtils.ts
@@ -10,7 +10,7 @@ import { DateField } from '../../fields/DateField';
import { Doc, DocListCast, Field, FieldResult, FieldType, LinkedTo, Opt, StrListCast } from '../../fields/Doc';
import { DocData } from '../../fields/DocSymbols';
import { Id } from '../../fields/FieldSymbols';
-import { InkDataFieldName, InkField } from '../../fields/InkField';
+import { InkData, InkDataFieldName, InkField } from '../../fields/InkField';
import { List, ListFieldName } from '../../fields/List';
import { ProxyField } from '../../fields/Proxy';
import { RichTextField } from '../../fields/RichTextField';
@@ -33,6 +33,10 @@ import { TaskCompletionBox } from '../views/nodes/TaskCompletedBox';
import { DocumentType } from './DocumentTypes';
import { Docs, DocumentOptions } from './Documents';
import { DocumentView } from '../views/nodes/DocumentView';
+import { INode, parse } from 'svgson';
+import { SVGToBezier, SVGType } from '../util/bezierFit';
+import { SmartDrawHandler } from '../views/smartdraw/SmartDrawHandler';
+import { PointData } from '../../pen-gestures/GestureTypes';
export namespace DocUtils {
function HasFunctionFilter(val: string) {
@@ -780,6 +784,57 @@ export namespace DocUtils {
return generatedDocuments;
}
+ export async function openSVGfile(file: File, options: DocumentOptions) {
+ const reader = new FileReader();
+ const scale = 1;
+ const startPoint = { X: (options.x as number) ?? 0, Y: (options.y as number) ?? 0 };
+ const buffer = await new Promise<string>((res, rej) => {
+ reader.onload = event => {
+ const fileContent = event.target?.result;
+ // Process the file content here
+ console.log(fileContent);
+ typeof fileContent === 'string' ? res(fileContent) : rej();
+ };
+
+ reader.readAsText(file);
+ });
+ const svg = buffer.match(/<svg[^>]*>([\s\S]*?)<\/svg>/g);
+ if (svg) {
+ const svgObject = await parse(svg[0]);
+ const strokeData: [InkData, string, string][] = [];
+ const tl = { X: Number.MAX_SAFE_INTEGER, Y: Number.MAX_SAFE_INTEGER };
+ let last: PointData = { X: 0, Y: 0 };
+ const processStroke = (child: INode) => {
+ child.attributes.d
+ .split(/[\n]?M/)
+ .slice(1)
+ .map((d, ind) => {
+ const convertedBezier: InkData = SVGToBezier(child.name as SVGType, { ...child, d: '\nM' + d } as unknown as Record<string, string>, last);
+ last = convertedBezier.lastElement();
+ convertedBezier.forEach(point => {
+ if (point.X < tl.X) tl.X = point.X;
+ if (point.Y < tl.Y) tl.Y = point.Y;
+ });
+ strokeData.push([convertedBezier, child.attributes.stroke || 'black', ind === 0 ? child.attributes.fill : child.attributes.fill === 'none' ? child.attributes.fill : DashColor(child.attributes.fill).negate().toString()]);
+ });
+ };
+ const processNode = (parent: INode) => {
+ if (parent.children.length) parent.children.forEach(processNode);
+ else if (parent.type !== 'text') processStroke(parent);
+ };
+ processNode(svgObject);
+
+ const mapStroke = (pd: PointData): PointData => ({ X: startPoint.X + (pd.X - tl.X) * scale, Y: startPoint.Y + (pd.Y - tl.Y) * scale });
+
+ return SmartDrawHandler.CreateDrawingDoc(
+ strokeData.map(sdata => [sdata[0].map(mapStroke), sdata[1], sdata[2]] as [PointData[], string, string]),
+ { autoColor: true },
+ '',
+ undefined
+ );
+ }
+ }
+
export function uploadFileToDoc(file: File, options: DocumentOptions, overwriteDoc: Doc) {
const generatedDocuments: Doc[] = [];
// Since this file has an overwriteDoc, we can set the client tracking guid to the overwriteDoc's guid.