@lvce-editor/preview-worker 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,6 +54,55 @@ class VError extends Error {
54
54
  }
55
55
  }
56
56
 
57
+ class AssertionError extends Error {
58
+ constructor(message) {
59
+ super(message);
60
+ this.name = 'AssertionError';
61
+ }
62
+ }
63
+ const Object$1 = 1;
64
+ const Number$1 = 2;
65
+ const Array$1 = 3;
66
+ const String = 4;
67
+ const Boolean = 5;
68
+ const Function = 6;
69
+ const Null = 7;
70
+ const Unknown = 8;
71
+ const getType = value => {
72
+ switch (typeof value) {
73
+ case 'number':
74
+ return Number$1;
75
+ case 'function':
76
+ return Function;
77
+ case 'string':
78
+ return String;
79
+ case 'object':
80
+ if (value === null) {
81
+ return Null;
82
+ }
83
+ if (Array.isArray(value)) {
84
+ return Array$1;
85
+ }
86
+ return Object$1;
87
+ case 'boolean':
88
+ return Boolean;
89
+ default:
90
+ return Unknown;
91
+ }
92
+ };
93
+ const array = value => {
94
+ const type = getType(value);
95
+ if (type !== Array$1) {
96
+ throw new AssertionError('expected value to be of type array');
97
+ }
98
+ };
99
+ const string = value => {
100
+ const type = getType(value);
101
+ if (type !== String) {
102
+ throw new AssertionError('expected value to be of type string');
103
+ }
104
+ };
105
+
57
106
  const isMessagePort = value => {
58
107
  return value && value instanceof MessagePort;
59
108
  };
@@ -854,8 +903,38 @@ const createMockRpc = ({
854
903
  return mockRpc;
855
904
  };
856
905
 
857
- const H1 = 5;
858
- const Text = 12;
906
+ const Div$1 = 4;
907
+ const H1$1 = 5;
908
+ const Span$1 = 8;
909
+ const Text$1 = 12;
910
+ const Img$1 = 17;
911
+ const H2$1 = 22;
912
+ const H3$1 = 23;
913
+ const H4$1 = 24;
914
+ const H5$1 = 25;
915
+ const Article$1 = 27;
916
+ const Aside$1 = 28;
917
+ const Footer$1 = 29;
918
+ const Header$1 = 30;
919
+ const Nav$1 = 40;
920
+ const Section$1 = 41;
921
+ const Search$1 = 42;
922
+ const Dd$1 = 43;
923
+ const Dl$1 = 44;
924
+ const Figcaption$1 = 45;
925
+ const Figure$1 = 46;
926
+ const Hr$1 = 47;
927
+ const Li$1 = 48;
928
+ const Ol$1 = 49;
929
+ const P$1 = 50;
930
+ const Pre$1 = 51;
931
+ const A$1 = 53;
932
+ const Abbr$1 = 54;
933
+ const Br$1 = 55;
934
+ const Cite$1 = 56;
935
+ const Data$1 = 57;
936
+ const Time$1 = 58;
937
+ const Tfoot$1 = 59;
859
938
  const Reference = 100;
860
939
 
861
940
  const TargetName = 'event.target.name';
@@ -914,8 +993,12 @@ const create$2 = rpcId => {
914
993
  };
915
994
 
916
995
  const {
996
+ invoke,
917
997
  set: set$1
918
998
  } = create$2(RendererWorker);
999
+ const readFile = async uri => {
1000
+ return invoke('FileSystem.readFile', uri);
1001
+ };
919
1002
 
920
1003
  const toCommandId = key => {
921
1004
  const dotIndex = key.indexOf('.');
@@ -1018,17 +1101,21 @@ const {
1018
1101
  const create = (uid, uri, x, y, width, height, platform, assetDir) => {
1019
1102
  const state = {
1020
1103
  assetDir,
1104
+ content: '',
1021
1105
  errorCount: 0,
1106
+ errorMessage: '',
1022
1107
  initial: true,
1108
+ parsedDom: [],
1023
1109
  platform,
1024
1110
  uid,
1111
+ uri,
1025
1112
  warningCount: 0
1026
1113
  };
1027
1114
  set(uid, state, state);
1028
1115
  };
1029
1116
 
1030
1117
  const isEqual = (oldState, newState) => {
1031
- return oldState.warningCount === newState.warningCount;
1118
+ return oldState.warningCount === newState.warningCount && oldState.initial === newState.initial && oldState.content === newState.content && oldState.parsedDom === newState.parsedDom;
1032
1119
  };
1033
1120
 
1034
1121
  const RenderItems = 4;
@@ -1057,12 +1144,11 @@ const diff2 = uid => {
1057
1144
  return result;
1058
1145
  };
1059
1146
 
1060
- const loadContent = async state => {
1147
+ const text = data => {
1061
1148
  return {
1062
- ...state,
1063
- errorCount: 0,
1064
- initial: false,
1065
- warningCount: 0
1149
+ childCount: 0,
1150
+ text: data,
1151
+ type: Text$1
1066
1152
  };
1067
1153
  };
1068
1154
 
@@ -1154,7 +1240,7 @@ const compareNodes = (oldNode, newNode) => {
1154
1240
  return patches;
1155
1241
  }
1156
1242
  // Handle text nodes
1157
- if (oldNode.type === Text && newNode.type === Text) {
1243
+ if (oldNode.type === Text$1 && newNode.type === Text$1) {
1158
1244
  if (oldNode.text !== newNode.text) {
1159
1245
  patches.push({
1160
1246
  type: SetText,
@@ -1354,21 +1440,548 @@ const diffTree = (oldNodes, newNodes) => {
1354
1440
  return removeTrailingNavigationPatches(patches);
1355
1441
  };
1356
1442
 
1357
- const getPreviewDom = () => {
1443
+ const Div = 'div';
1444
+ const H1 = 'h1';
1445
+ const H2 = 'h2';
1446
+ const H3 = 'h3';
1447
+ const H4 = 'h4';
1448
+ const H5 = 'h5';
1449
+ const Img = 'img';
1450
+ const Span = 'span';
1451
+ const Article = 'article';
1452
+ const Aside = 'aside';
1453
+ const Footer = 'footer';
1454
+ const Header = 'header';
1455
+ const Nav = 'nav';
1456
+ const Section = 'section';
1457
+ const Search = 'search';
1458
+ const Dd = 'dd';
1459
+ const Dl = 'dl';
1460
+ const Figcaption = 'figcaption';
1461
+ const Figure = 'figure';
1462
+ const Hr = 'hr';
1463
+ const Li = 'li';
1464
+ const Ol = 'ol';
1465
+ const P = 'p';
1466
+ const Pre = 'pre';
1467
+ const A = 'a';
1468
+ const Abbr = 'abbr';
1469
+ const Br = 'br';
1470
+ const Cite = 'cite';
1471
+ const Data = 'data';
1472
+ const Time = 'time';
1473
+ const Tfoot = 'tfoot';
1474
+
1475
+ const getVirtualDomTag = text => {
1476
+ switch (text) {
1477
+ case A:
1478
+ return A$1;
1479
+ case Abbr:
1480
+ return Abbr$1;
1481
+ case Article:
1482
+ return Article$1;
1483
+ case Aside:
1484
+ return Aside$1;
1485
+ case Br:
1486
+ return Br$1;
1487
+ case Cite:
1488
+ return Cite$1;
1489
+ case Data:
1490
+ return Data$1;
1491
+ case Dd:
1492
+ return Dd$1;
1493
+ case Div:
1494
+ return Div$1;
1495
+ case Dl:
1496
+ return Dl$1;
1497
+ case Figcaption:
1498
+ return Figcaption$1;
1499
+ case Figure:
1500
+ return Figure$1;
1501
+ case Footer:
1502
+ return Footer$1;
1503
+ case H1:
1504
+ return H1$1;
1505
+ case H2:
1506
+ return H2$1;
1507
+ case H3:
1508
+ return H3$1;
1509
+ case H4:
1510
+ return H4$1;
1511
+ case H5:
1512
+ return H5$1;
1513
+ case Header:
1514
+ return Header$1;
1515
+ case Hr:
1516
+ return Hr$1;
1517
+ case Img:
1518
+ return Img$1;
1519
+ case Li:
1520
+ return Li$1;
1521
+ case Nav:
1522
+ return Nav$1;
1523
+ case Ol:
1524
+ return Ol$1;
1525
+ case P:
1526
+ return P$1;
1527
+ case Pre:
1528
+ return Pre$1;
1529
+ case Search:
1530
+ return Search$1;
1531
+ case Section:
1532
+ return Section$1;
1533
+ case Span:
1534
+ return Span$1;
1535
+ case Tfoot:
1536
+ return Tfoot$1;
1537
+ case Time:
1538
+ return Time$1;
1539
+ default:
1540
+ return Div$1;
1541
+ }
1542
+ };
1543
+
1544
+ const None = 0;
1545
+ const OpeningAngleBracket = 1;
1546
+ const ClosingAngleBracket = 2;
1547
+ const TagNameStart = 3;
1548
+ const TagNameEnd = 4;
1549
+ const Content = 5;
1550
+ const ClosingTagSlash = 6;
1551
+ const WhitespaceInsideOpeningTag = 7;
1552
+ const AttributeName = 8;
1553
+ const AttributeEqualSign = 9;
1554
+ const AttributeQuoteStart = 10;
1555
+ const AttributeValue = 11;
1556
+ const AttributeQuoteEnd = 12;
1557
+ const WhitespaceAfterClosingTagSlash = 13;
1558
+ const WhitespaceAfterOpeningTagOpenAngleBracket = 14;
1559
+ const ExclamationMark = 15;
1560
+ const Doctype = 16;
1561
+ const StartCommentDashes = 17;
1562
+ const Comment = 18;
1563
+ const EndCommentTag = 19;
1564
+ const Text = 20;
1565
+ const CommentStart = 21;
1566
+
1567
+ const isSelfClosingTag = tag => {
1568
+ switch (tag) {
1569
+ case Img:
1570
+ return true;
1571
+ default:
1572
+ return false;
1573
+ }
1574
+ };
1575
+
1576
+ const parseText = text => {
1577
+ return text.replaceAll('&gt;', '>').replaceAll('&lt;', '<').replaceAll('&amp;', '&');
1578
+ };
1579
+
1580
+ class UnexpectedTokenError extends Error {
1581
+ constructor() {
1582
+ super('Unexpected token');
1583
+ this.name = 'UnexpectedTokenError';
1584
+ }
1585
+ }
1586
+
1587
+ const State = {
1588
+ AfterAttributeEqualSign: 8,
1589
+ AfterAttributeName: 7,
1590
+ AfterAttributeValueClosingQuote: 11,
1591
+ AfterAttributeValueInsideDoubleQuote: 10,
1592
+ AfterClosingTagName: 5,
1593
+ AfterClosingTagSlash: 4,
1594
+ AfterExclamationMark: 16,
1595
+ AfterOpeningAngleBracket: 2,
1596
+ InsideAttributeAfterDoubleQuote: 9,
1597
+ InsideComment: 17,
1598
+ InsideOpeningTag: 3,
1599
+ InsideOpeningTagAfterWhitespace: 6,
1600
+ TopLevelContent: 1
1601
+ };
1602
+ const RE_ANGLE_BRACKET_OPEN = /^</;
1603
+ const RE_ANGLE_BRACKET_OPEN_TAG = /^<(?![\s!%])/;
1604
+ const RE_ANGLE_BRACKET_CLOSE = /^>/;
1605
+ const RE_SLASH = /^\//;
1606
+ const RE_TAGNAME = /^[a-zA-Z\d$]+/;
1607
+ const RE_CONTENT = /^[^<>]+/;
1608
+ const RE_WHITESPACE = /^\s+/;
1609
+ const RE_ATTRIBUTE_NAME = /^[a-zA-Z\d-]+/;
1610
+ const RE_EQUAL_SIGN = /^=/;
1611
+ const RE_DOUBLE_QUOTE = /^"/;
1612
+ const RE_ATTRIBUTE_VALUE_INSIDE_DOUBLE_QUOTE = /^[^"\n]+/;
1613
+ const RE_TEXT = /^[^<>]+/;
1614
+ const RE_EXCLAMATION_MARK = /^!/;
1615
+ const RE_DASH_DASH = /^--/;
1616
+ const RE_DOCTYPE = /^doctype/i;
1617
+ const RE_BLOCK_COMMENT_CONTENT = /^[a-zA-Z\s]+/;
1618
+ const RE_COMMENT_END = /^-->/;
1619
+ const RE_TAG_TEXT = /^[^\s>]+/;
1620
+ const RE_ANY_TEXT = /^[^\n]+/;
1621
+ const RE_ATTRIBUTE_TEXT = /^[^\n<>/\s]+/;
1622
+ const RE_BLOCK_COMMENT_START = /^<!--/;
1623
+ const RE_SELF_CLOSING = /^\/>/;
1624
+ const tokenizeHtml = text => {
1625
+ string(text);
1626
+ let state = State.TopLevelContent;
1627
+ let index = 0;
1628
+ let next;
1629
+ const tokens = [];
1630
+ let token = None;
1631
+ while (index < text.length) {
1632
+ const part = text.slice(index);
1633
+ switch (state) {
1634
+ case State.AfterAttributeEqualSign:
1635
+ if (next = part.match(RE_DOUBLE_QUOTE)) {
1636
+ token = AttributeQuoteStart;
1637
+ state = State.InsideAttributeAfterDoubleQuote;
1638
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1639
+ token = ClosingAngleBracket;
1640
+ state = State.TopLevelContent;
1641
+ } else if (next = part.match(RE_ATTRIBUTE_TEXT)) {
1642
+ token = AttributeValue;
1643
+ state = State.InsideOpeningTag;
1644
+ } else {
1645
+ throw new UnexpectedTokenError();
1646
+ }
1647
+ break;
1648
+ case State.AfterAttributeName:
1649
+ if (next = part.match(RE_EQUAL_SIGN)) {
1650
+ token = AttributeEqualSign;
1651
+ state = State.AfterAttributeEqualSign;
1652
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1653
+ token = ClosingAngleBracket;
1654
+ state = State.TopLevelContent;
1655
+ } else if (next = part.match(RE_WHITESPACE)) {
1656
+ token = WhitespaceInsideOpeningTag;
1657
+ state = State.InsideOpeningTagAfterWhitespace;
1658
+ } else if (next = part.match(RE_ANGLE_BRACKET_OPEN)) {
1659
+ token = OpeningAngleBracket;
1660
+ state = State.AfterOpeningAngleBracket;
1661
+ } else {
1662
+ text.slice(index); // ?
1663
+ throw new UnexpectedTokenError();
1664
+ }
1665
+ break;
1666
+ case State.AfterAttributeValueClosingQuote:
1667
+ if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1668
+ token = ClosingAngleBracket;
1669
+ state = State.TopLevelContent;
1670
+ } else if (next = part.match(RE_WHITESPACE)) {
1671
+ token = WhitespaceInsideOpeningTag;
1672
+ state = State.InsideOpeningTagAfterWhitespace;
1673
+ } else if (next = part.match(RE_SELF_CLOSING)) {
1674
+ token = ClosingAngleBracket;
1675
+ state = State.TopLevelContent;
1676
+ } else {
1677
+ throw new UnexpectedTokenError();
1678
+ }
1679
+ break;
1680
+ case State.AfterAttributeValueInsideDoubleQuote:
1681
+ if (next = part.match(RE_DOUBLE_QUOTE)) {
1682
+ token = AttributeQuoteEnd;
1683
+ state = State.AfterAttributeValueClosingQuote;
1684
+ } else {
1685
+ throw new UnexpectedTokenError();
1686
+ }
1687
+ break;
1688
+ case State.AfterClosingTagName:
1689
+ if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1690
+ token = ClosingAngleBracket;
1691
+ state = State.TopLevelContent;
1692
+ } else if (next = part.match(RE_WHITESPACE)) {
1693
+ token = Content;
1694
+ state = State.TopLevelContent;
1695
+ } else {
1696
+ throw new UnexpectedTokenError();
1697
+ }
1698
+ break;
1699
+ case State.AfterClosingTagSlash:
1700
+ if (next = part.match(RE_TAGNAME)) {
1701
+ token = TagNameEnd;
1702
+ state = State.AfterClosingTagName;
1703
+ } else if (next = part.match(RE_WHITESPACE)) {
1704
+ token = WhitespaceAfterClosingTagSlash;
1705
+ state = State.TopLevelContent;
1706
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1707
+ token = ClosingAngleBracket;
1708
+ state = State.TopLevelContent;
1709
+ } else {
1710
+ throw new UnexpectedTokenError();
1711
+ }
1712
+ break;
1713
+ case State.AfterExclamationMark:
1714
+ if (next = part.match(RE_DASH_DASH)) {
1715
+ token = StartCommentDashes;
1716
+ state = State.InsideComment;
1717
+ } else if (next = part.match(RE_DOCTYPE)) {
1718
+ token = Doctype;
1719
+ state = State.InsideOpeningTag;
1720
+ } else {
1721
+ text.slice(index); // ?
1722
+ throw new UnexpectedTokenError();
1723
+ }
1724
+ break;
1725
+ case State.AfterOpeningAngleBracket:
1726
+ if (next = part.match(RE_TAGNAME)) {
1727
+ token = TagNameStart;
1728
+ state = State.InsideOpeningTag;
1729
+ } else if (next = part.match(RE_SLASH)) {
1730
+ token = ClosingTagSlash;
1731
+ state = State.AfterClosingTagSlash;
1732
+ } else if (next = part.match(RE_WHITESPACE)) {
1733
+ token = WhitespaceAfterOpeningTagOpenAngleBracket;
1734
+ state = State.TopLevelContent;
1735
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1736
+ token = ClosingAngleBracket;
1737
+ state = State.TopLevelContent;
1738
+ } else if (next = part.match(RE_EXCLAMATION_MARK)) {
1739
+ token = ExclamationMark;
1740
+ state = State.AfterExclamationMark;
1741
+ } else if (next = part.match(RE_ANY_TEXT)) {
1742
+ token = Text;
1743
+ state = State.TopLevelContent;
1744
+ } else {
1745
+ text.slice(index); // ?
1746
+ throw new UnexpectedTokenError();
1747
+ }
1748
+ break;
1749
+ case State.InsideAttributeAfterDoubleQuote:
1750
+ if (next = text.slice(index).match(RE_ATTRIBUTE_VALUE_INSIDE_DOUBLE_QUOTE)) {
1751
+ token = AttributeValue;
1752
+ state = State.AfterAttributeValueInsideDoubleQuote;
1753
+ } else if (next = part.match(RE_DOUBLE_QUOTE)) {
1754
+ token = AttributeQuoteEnd;
1755
+ state = State.AfterAttributeValueClosingQuote;
1756
+ } else {
1757
+ throw new UnexpectedTokenError();
1758
+ }
1759
+ break;
1760
+ case State.InsideComment:
1761
+ if (next = part.match(RE_BLOCK_COMMENT_CONTENT)) {
1762
+ token = Comment;
1763
+ state = State.InsideComment;
1764
+ } else if (next = part.match(RE_COMMENT_END)) {
1765
+ token = EndCommentTag;
1766
+ state = State.TopLevelContent;
1767
+ } else {
1768
+ text.slice(index); // ?
1769
+ throw new UnexpectedTokenError();
1770
+ }
1771
+ break;
1772
+ case State.InsideOpeningTag:
1773
+ if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1774
+ token = ClosingAngleBracket;
1775
+ state = State.TopLevelContent;
1776
+ } else if (next = part.match(RE_WHITESPACE)) {
1777
+ token = WhitespaceInsideOpeningTag;
1778
+ state = State.InsideOpeningTagAfterWhitespace;
1779
+ } else if (next = part.match(RE_TAG_TEXT)) {
1780
+ token = Text;
1781
+ state = State.TopLevelContent;
1782
+ } else {
1783
+ throw new UnexpectedTokenError();
1784
+ }
1785
+ break;
1786
+ case State.InsideOpeningTagAfterWhitespace:
1787
+ if (next = part.match(RE_ATTRIBUTE_NAME)) {
1788
+ token = AttributeName;
1789
+ state = State.AfterAttributeName;
1790
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1791
+ token = ClosingAngleBracket;
1792
+ state = State.TopLevelContent;
1793
+ } else if (next = part.match(RE_SELF_CLOSING)) {
1794
+ token = ClosingAngleBracket;
1795
+ state = State.TopLevelContent;
1796
+ } else if (next = part.match(RE_TEXT)) {
1797
+ token = AttributeName;
1798
+ state = State.AfterAttributeName;
1799
+ } else {
1800
+ text.slice(index).match(RE_TEXT); // ?
1801
+ text.slice(index); // ?
1802
+ throw new UnexpectedTokenError();
1803
+ }
1804
+ break;
1805
+ case State.TopLevelContent:
1806
+ if (next = part.match(RE_ANGLE_BRACKET_OPEN_TAG)) {
1807
+ token = OpeningAngleBracket;
1808
+ state = State.AfterOpeningAngleBracket;
1809
+ } else if (next = part.match(RE_CONTENT)) {
1810
+ token = Content;
1811
+ state = State.TopLevelContent;
1812
+ } else if (next = part.match(RE_BLOCK_COMMENT_START)) {
1813
+ token = CommentStart;
1814
+ state = State.InsideComment;
1815
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1816
+ token = Content;
1817
+ state = State.TopLevelContent;
1818
+ } else if (next = part.match(RE_ANGLE_BRACKET_OPEN)) {
1819
+ token = Text;
1820
+ state = State.TopLevelContent;
1821
+ } else {
1822
+ throw new UnexpectedTokenError();
1823
+ }
1824
+ break;
1825
+ default:
1826
+ throw new UnexpectedTokenError();
1827
+ }
1828
+ const tokenText = next[0];
1829
+ tokens.push({
1830
+ text: tokenText,
1831
+ type: token
1832
+ });
1833
+ index += tokenText.length;
1834
+ }
1835
+ return tokens;
1836
+ };
1837
+
1838
+ const parseHtml = (html, allowedAttributes) => {
1839
+ string(html);
1840
+ array(allowedAttributes);
1841
+ const tokens = tokenizeHtml(html);
1842
+ const dom = [];
1843
+ const root = {
1844
+ childCount: 0,
1845
+ type: 0
1846
+ };
1847
+ let current = root;
1848
+ const stack = [root];
1849
+ let attributeName = '';
1850
+ for (const token of tokens) {
1851
+ switch (token.type) {
1852
+ case AttributeName:
1853
+ attributeName = token.text;
1854
+ if (attributeName === 'class') {
1855
+ attributeName = 'className';
1856
+ }
1857
+ break;
1858
+ case AttributeValue:
1859
+ if (allowedAttributes.includes(attributeName)) {
1860
+ current[attributeName] = token.text;
1861
+ }
1862
+ attributeName = '';
1863
+ break;
1864
+ case Content:
1865
+ current.childCount++;
1866
+ dom.push(text(parseText(token.text)));
1867
+ break;
1868
+ case TagNameEnd:
1869
+ stack.pop();
1870
+ current = stack.at(-1) || root;
1871
+ break;
1872
+ case TagNameStart:
1873
+ current.childCount++;
1874
+ current = {
1875
+ childCount: 0,
1876
+ type: getVirtualDomTag(token.text)
1877
+ };
1878
+ dom.push(current);
1879
+ if (!isSelfClosingTag(token.text)) {
1880
+ stack.push(current);
1881
+ }
1882
+ break;
1883
+ }
1884
+ }
1885
+ return dom;
1886
+ };
1887
+
1888
+ const updateContent = async (state, uri) => {
1889
+ try {
1890
+ // Read the file content using RendererWorker RPC
1891
+ // @ts-ignore
1892
+ const content = await readFile(uri);
1893
+
1894
+ // Parse the content into virtual DOM
1895
+ const parsedDom = parseHtml(content, []);
1896
+ return {
1897
+ content,
1898
+ errorMessage: '',
1899
+ parsedDom
1900
+ };
1901
+ } catch (error) {
1902
+ // If file reading or parsing fails, return empty content and parsedDom with error message
1903
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error';
1904
+ return {
1905
+ content: '',
1906
+ errorMessage,
1907
+ parsedDom: []
1908
+ };
1909
+ }
1910
+ };
1911
+
1912
+ const handleFileEdited = async state => {
1913
+ const {
1914
+ content,
1915
+ errorMessage,
1916
+ parsedDom
1917
+ } = await updateContent(state, state.uri);
1918
+ return {
1919
+ ...state,
1920
+ content,
1921
+ errorMessage,
1922
+ parsedDom
1923
+ };
1924
+ };
1925
+
1926
+ const loadContent = async state => {
1927
+ return {
1928
+ ...state,
1929
+ errorCount: 0,
1930
+ initial: false,
1931
+ warningCount: 1
1932
+ };
1933
+ };
1934
+
1935
+ const getEmptyPreviewDom = () => {
1358
1936
  return [{
1359
1937
  childCount: 1,
1360
- type: H1
1938
+ className: 'Viewlet Preview',
1939
+ type: Div$1
1940
+ }, {
1941
+ childCount: 1,
1942
+ type: H1$1
1943
+ }, {
1944
+ text: 'No URI has been specified',
1945
+ type: Text$1
1946
+ }];
1947
+ };
1948
+
1949
+ const getPreviewDom = state => {
1950
+ if (!state.uri) {
1951
+ return getEmptyPreviewDom();
1952
+ }
1953
+
1954
+ // If parsedDom is available, render it as children of the wrapper
1955
+ if (state.parsedDom && state.parsedDom.length > 0) {
1956
+ return [{
1957
+ childCount: 1,
1958
+ // TODO
1959
+ className: 'Viewlet Preview',
1960
+ type: Div$1
1961
+ }, ...state.parsedDom];
1962
+ }
1963
+ return [{
1964
+ childCount: 1,
1965
+ className: 'Viewlet Preview',
1966
+ type: Div$1
1967
+ }, {
1968
+ childCount: 1,
1969
+ type: H1$1
1361
1970
  }, {
1362
1971
  text: 'hello from preview',
1363
- type: Text
1972
+ type: Text$1
1364
1973
  }];
1365
1974
  };
1366
1975
 
1367
1976
  const renderItems = (oldState, newState) => {
1368
1977
  const {
1978
+ initial,
1369
1979
  uid
1370
1980
  } = newState;
1371
- const dom = getPreviewDom();
1981
+ if (initial) {
1982
+ return [SetDom2, uid, []];
1983
+ }
1984
+ const dom = getPreviewDom(newState);
1372
1985
  return [SetDom2, uid, dom];
1373
1986
  };
1374
1987
 
@@ -1434,15 +2047,32 @@ const saveState = state => {
1434
2047
  };
1435
2048
  };
1436
2049
 
2050
+ const setUri = async (state, uri) => {
2051
+ const {
2052
+ content,
2053
+ errorMessage,
2054
+ parsedDom
2055
+ } = await updateContent(state, uri);
2056
+ return {
2057
+ ...state,
2058
+ content,
2059
+ errorMessage,
2060
+ parsedDom,
2061
+ uri
2062
+ };
2063
+ };
2064
+
1437
2065
  const commandMap = {
1438
2066
  'Preview.create': create,
1439
2067
  'Preview.diff2': diff2,
1440
2068
  'Preview.getCommandIds': getCommandIds,
2069
+ 'Preview.handleFileEdited': wrapCommand(handleFileEdited),
1441
2070
  'Preview.loadContent': wrapCommand(loadContent),
1442
2071
  'Preview.render2': render2,
1443
2072
  'Preview.renderEventListeners': renderEventListeners,
1444
2073
  'Preview.resize': wrapCommand(resize),
1445
2074
  'Preview.saveState': wrapGetter(saveState),
2075
+ 'Preview.setUri': wrapCommand(setUri),
1446
2076
  'Preview.terminate': terminate
1447
2077
  };
1448
2078
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/preview-worker",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Preview Worker",
5
5
  "repository": {
6
6
  "type": "git",