@swiftbrowser/native 0.19.0 → 0.23.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.
Files changed (66) hide show
  1. package/README.md +34 -11
  2. package/dist/blob.d.ts +85 -11
  3. package/dist/blob.d.ts.map +1 -1
  4. package/dist/blob.js +184 -34
  5. package/dist/blob.js.map +1 -1
  6. package/dist/bridge.d.ts +33 -2
  7. package/dist/bridge.d.ts.map +1 -1
  8. package/dist/bridge.js.map +1 -1
  9. package/dist/fetch.d.ts +19 -11
  10. package/dist/fetch.d.ts.map +1 -1
  11. package/dist/fetch.js +105 -41
  12. package/dist/fetch.js.map +1 -1
  13. package/dist/hostRequests.d.ts +3 -1
  14. package/dist/hostRequests.d.ts.map +1 -1
  15. package/dist/hostRequests.js +47 -6
  16. package/dist/hostRequests.js.map +1 -1
  17. package/dist/index.d.ts +1 -2
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +1 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/runtime.js +1 -1
  22. package/dist/runtime.js.map +1 -1
  23. package/dist/webSocket.d.ts.map +1 -1
  24. package/dist/webSocket.js +13 -1
  25. package/dist/webSocket.js.map +1 -1
  26. package/package.json +3 -3
  27. package/src/blob.ts +215 -34
  28. package/src/bridge.ts +27 -2
  29. package/src/fetch.ts +99 -41
  30. package/src/hostRequests.ts +62 -8
  31. package/src/index.ts +1 -2
  32. package/src/runtime.ts +1 -1
  33. package/src/webSocket.ts +10 -1
  34. package/swift/Packages/SwiftBrowserDSP/Sources/SwiftBrowserDSP/ProgramNode.swift +185 -24
  35. package/swift/Packages/SwiftBrowserDSP/Sources/SwiftBrowserDSP/ScopeView.swift +79 -0
  36. package/swift/Packages/SwiftBrowserDSP/Sources/SwiftBrowserDSPCore/Machine.swift +2075 -213
  37. package/swift/Packages/SwiftBrowserDSP/Sources/SwiftBrowserDSPCore/Program.swift +407 -16
  38. package/swift/Packages/SwiftBrowserShader/Package.swift +31 -0
  39. package/swift/Packages/SwiftBrowserShader/README.md +45 -0
  40. package/swift/Packages/SwiftBrowserShader/Sources/SwiftBrowserShader/ShaderRenderer.swift +103 -0
  41. package/swift/Packages/SwiftBrowserShader/Sources/SwiftBrowserShader/ShaderView.swift +168 -0
  42. package/swift/Packages/SwiftBrowserShader/Sources/SwiftBrowserShaderCore/ShaderProps.swift +120 -0
  43. package/swift/Sources/SwiftBrowserNative/Host/BodyStream.swift +166 -0
  44. package/swift/Sources/SwiftBrowserNative/Host/FilesBridge.swift +141 -7
  45. package/swift/Sources/SwiftBrowserNative/Host/HostBridge.swift +47 -1
  46. package/swift/Sources/SwiftBrowserNative/Host/HostRequestBridge.swift +98 -29
  47. package/swift/Sources/SwiftBrowserNative/Runtime/NativeFunctions.swift +1 -1
  48. package/swift/Sources/SwiftBrowserNative/Runtime/ViewRegistry.swift +57 -0
  49. package/swift/Sources/SwiftBrowserNative/Views/ElementView.swift +11 -7
  50. package/swift/Sources/SwiftBrowserNative/Views/Kinds/Controls.swift +28 -6
  51. package/swift/Sources/SwiftBrowserNative/Views/Kinds/Drawing.swift +378 -0
  52. package/swift/Sources/SwiftBrowserNative/Views/Kinds/Navigation.swift +15 -10
  53. package/swift/Sources/SwiftBrowserNative/Views/Kinds/PopTap.swift +31 -28
  54. package/swift/Sources/SwiftBrowserNative/Views/StyleModifier.swift +142 -0
  55. package/swift/Sources/SwiftBrowserProtocol/Drawing.swift +306 -0
  56. package/swift/Sources/SwiftBrowserProtocol/Props.swift +5 -2
  57. package/swift/Sources/SwiftBrowserProtocol/RendererEvent.swift +24 -3
  58. package/swift/Sources/SwiftBrowserProtocol/Style.swift +87 -0
  59. package/swift/Sources/SwiftBrowserProtocol/TextChange.swift +17 -0
  60. package/wasm/SwiftBrowserNative.wasm +2 -2
  61. package/dist/native.d.ts +0 -19
  62. package/dist/native.d.ts.map +0 -1
  63. package/dist/native.js +0 -17
  64. package/dist/native.js.map +0 -1
  65. package/src/native.ts +0 -35
  66. package/swift/Sources/SwiftBrowserNative/Runtime/NativeRegistry.swift +0 -53
@@ -0,0 +1,17 @@
1
+ /// Where a caret goes when a text changes under it from outside (another
2
+ /// person's edit arriving in a `texteditor`): an offset before the change
3
+ /// (the common prefix of the two texts) stays; one after it (the common
4
+ /// suffix) moves by the change's length; one inside the change goes to the
5
+ /// change's end. Offsets are UTF-16 units, as a `selection` is on the wire.
6
+ public func caretAfterChange(before: String, after: String, offset: Int) -> Int {
7
+ let a = Array(before.utf16)
8
+ let b = Array(after.utf16)
9
+ let shortest = min(a.count, b.count)
10
+ var prefix = 0
11
+ while prefix < shortest, a[prefix] == b[prefix] { prefix += 1 }
12
+ var suffix = 0
13
+ while suffix < shortest - prefix, a[a.count - 1 - suffix] == b[b.count - 1 - suffix] { suffix += 1 }
14
+ if offset <= prefix { return offset }
15
+ if offset >= a.count - suffix { return offset + (b.count - a.count) }
16
+ return b.count - suffix
17
+ }
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/wasm/SwiftBrowserNative.wasm
3
- size: 50133314 bytes
4
- sha256: abeb48c78246add67a36e017b71c7fadc7f42bc58ac90f0b4808d73ab3ac7982
3
+ size: 50265117 bytes
4
+ sha256: f7cb92276a436e8c765a5eca556c7436d02752c411256b0d6ff4e2ca33950635
package/dist/native.d.ts DELETED
@@ -1,19 +0,0 @@
1
- /**
2
- * `<Native name props onEvent>`: the escape hatch (docs/native.md). The
3
- * element kind `native`, which only this producer emits, names a SwiftUI view
4
- * the app registered with `NativeRegistry.register(name)`; `props` reach it
5
- * as JSON, its children as SwiftUI views, and what it sends back arrives at
6
- * `onEvent`.
7
- */
8
- import { type ReactElement, type ReactNode } from 'react';
9
- export interface NativeProps {
10
- /** The name registered on the Swift side. */
11
- name: string;
12
- /** JSON-serializable props for the view. */
13
- props?: Record<string, unknown>;
14
- /** A value the native view sent (`context.send(value)`). */
15
- onEvent?: (value: unknown) => void;
16
- children?: ReactNode;
17
- }
18
- export declare function Native({ name, props, onEvent, children }: NativeProps): ReactElement;
19
- //# sourceMappingURL=native.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"native.d.ts","sourceRoot":"","sources":["../src/native.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAiB,KAAK,YAAY,EAAE,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAEzE,MAAM,WAAW,WAAW;IAC1B,6CAA6C;IAC7C,IAAI,EAAE,MAAM,CAAC;IACb,4CAA4C;IAC5C,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,4DAA4D;IAC5D,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;IACnC,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAUD,wBAAgB,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,WAAW,GAAG,YAAY,CAMpF"}
package/dist/native.js DELETED
@@ -1,17 +0,0 @@
1
- /**
2
- * `<Native name props onEvent>`: the escape hatch (docs/native.md). The
3
- * element kind `native`, which only this producer emits, names a SwiftUI view
4
- * the app registered with `NativeRegistry.register(name)`; `props` reach it
5
- * as JSON, its children as SwiftUI views, and what it sends back arrives at
6
- * `onEvent`.
7
- */
8
- import { createElement } from 'react';
9
- export function Native({ name, props, onEvent, children }) {
10
- // The host element of @swiftbrowser/react: `sb` with the protocol kind and
11
- // props, and the handlers by event type (packages/react/src/host/instance.ts).
12
- const hostProps = { kind: 'native', props: { name, props: props ?? {} } };
13
- if (onEvent)
14
- hostProps['on'] = { native: (event) => onEvent(event.value) };
15
- return children === undefined ? createElement('sb', hostProps) : createElement('sb', hostProps, children);
16
- }
17
- //# sourceMappingURL=native.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"native.js","sourceRoot":"","sources":["../src/native.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,aAAa,EAAqC,MAAM,OAAO,CAAC;AAoBzE,MAAM,UAAU,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAe;IACpE,2EAA2E;IAC3E,+EAA+E;IAC/E,MAAM,SAAS,GAA4B,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,IAAI,EAAE,EAAE,EAAE,CAAC;IACnG,IAAI,OAAO;QAAE,SAAS,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,CAAC,KAAkB,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;IACxF,OAAO,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;AAC5G,CAAC"}
package/src/native.ts DELETED
@@ -1,35 +0,0 @@
1
- /**
2
- * `<Native name props onEvent>`: the escape hatch (docs/native.md). The
3
- * element kind `native`, which only this producer emits, names a SwiftUI view
4
- * the app registered with `NativeRegistry.register(name)`; `props` reach it
5
- * as JSON, its children as SwiftUI views, and what it sends back arrives at
6
- * `onEvent`.
7
- */
8
-
9
- import { createElement, type ReactElement, type ReactNode } from 'react';
10
-
11
- export interface NativeProps {
12
- /** The name registered on the Swift side. */
13
- name: string;
14
- /** JSON-serializable props for the view. */
15
- props?: Record<string, unknown>;
16
- /** A value the native view sent (`context.send(value)`). */
17
- onEvent?: (value: unknown) => void;
18
- children?: ReactNode;
19
- }
20
-
21
- /** The shape of a `native` event (`RendererEvent` has no such member). */
22
- interface NativeEvent {
23
- type: 'native';
24
- id: number;
25
- name: string;
26
- value: unknown;
27
- }
28
-
29
- export function Native({ name, props, onEvent, children }: NativeProps): ReactElement {
30
- // The host element of @swiftbrowser/react: `sb` with the protocol kind and
31
- // props, and the handlers by event type (packages/react/src/host/instance.ts).
32
- const hostProps: Record<string, unknown> = { kind: 'native', props: { name, props: props ?? {} } };
33
- if (onEvent) hostProps['on'] = { native: (event: NativeEvent) => onEvent(event.value) };
34
- return children === undefined ? createElement('sb', hostProps) : createElement('sb', hostProps, children);
35
- }
@@ -1,53 +0,0 @@
1
- #if canImport(SwiftUI)
2
- import Foundation
3
- import SwiftBrowserProtocol
4
- import SwiftBrowserTree
5
- import SwiftUI
6
-
7
- /// What a registered native view gets: the element's `props` as the app
8
- /// wrote them, its children (SwiftUI views the app put inside it) and a way
9
- /// to send a value back, which reaches the element's `onEvent` in JavaScript.
10
- public struct NativeViewContext {
11
- public let id: Int
12
- public let props: JSONValue
13
- public let children: [ElementNode]
14
- public let send: (JSONValue) -> Void
15
-
16
- /// The children as a view (built on the main actor, as views are).
17
- @MainActor public var content: some View {
18
- ForEach(children) { ElementView(node: $0) }
19
- }
20
- }
21
-
22
- /// The escape hatch (docs/native.md): the `native` element kind, which only
23
- /// a JavaScript producer emits, names a view the Swift app registered.
24
- ///
25
- /// ```swift
26
- /// NativeRegistry.register("MapView") { context in
27
- /// MapView(region: context.props["region"]) { context.send(["tapped": true]) }
28
- /// }
29
- /// ```
30
- ///
31
- /// ```tsx
32
- /// <Native name="MapView" props={{ region }} onEvent={(value) => …} />
33
- /// ```
34
- @MainActor
35
- public enum NativeRegistry {
36
- public typealias Builder = (NativeViewContext) -> AnyView
37
- private static var builders: [String: Builder] = [:]
38
-
39
- public static func register<V: View>(_ name: String, @ViewBuilder _ builder: @escaping (NativeViewContext) -> V) {
40
- builders[name] = { AnyView(builder($0)) }
41
- }
42
-
43
- public static func unregister(_ name: String) {
44
- builders[name] = nil
45
- }
46
-
47
- public static func isRegistered(_ name: String) -> Bool { builders[name] != nil }
48
-
49
- static func view(name: String, context: NativeViewContext) -> AnyView? {
50
- builders[name]?(context)
51
- }
52
- }
53
- #endif