├── .eslintrc.js ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── src ├── compiler │ ├── compile.ts │ ├── compileVugel.ts │ ├── makeMap.ts │ └── parserOptionsMinimal.ts ├── events │ ├── focusEvents.ts │ ├── hover.ts │ ├── index.ts │ ├── keyboardEvents.ts │ ├── mouseEvents.ts │ ├── touchEvents.ts │ ├── types.ts │ └── utils.ts ├── index.ts ├── runtime │ ├── index.ts │ ├── nodeOps.ts │ ├── nodes │ │ ├── Base.ts │ │ ├── Comment.ts │ │ ├── Container.ts │ │ ├── DirectContainer.ts │ │ ├── Node.ts │ │ ├── Paragraph.ts │ │ ├── Root.ts │ │ ├── TextNode.ts │ │ ├── direct.ts │ │ ├── effects │ │ │ ├── BoxBlur.ts │ │ │ ├── Grayscale.ts │ │ │ ├── Rounded.ts │ │ │ ├── Shader.ts │ │ │ └── index.ts │ │ ├── index.ts │ │ ├── textures │ │ │ ├── Drawing.ts │ │ │ ├── DynamicSizeTexture.ts │ │ │ ├── Picture.ts │ │ │ ├── Rectangle.ts │ │ │ ├── StyledRectangle.ts │ │ │ ├── Svg.ts │ │ │ ├── TextTexture.ts │ │ │ ├── TextTextureSettings.ts │ │ │ ├── Texture.ts │ │ │ └── index.ts │ │ ├── tree2dEvents.ts │ │ └── types.ts │ ├── patchProp.ts │ └── utils │ │ ├── Delegator.ts │ │ ├── TypeUtils.ts │ │ ├── index.ts │ │ └── mixin.ts └── wrapper.ts ├── tsconfig.json ├── webpack.config.js └── yarn.lock /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: ["@planning.nl"], 3 | }; 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | cjs/ 2 | esm/ 3 | 4 | # We don't need to share config 5 | .idea 6 | 7 | # Created by https://www.gitignore.io/api/node,intellij 8 | # Edit at https://www.gitignore.io/?templates=node,intellij 9 | 10 | ### Intellij ### 11 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 12 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 13 | 14 | # User-specific stuff 15 | .idea/**/workspace.xml 16 | .idea/**/tasks.xml 17 | .idea/**/usage.statistics.xml 18 | .idea/**/dictionaries 19 | .idea/**/shelf 20 | 21 | # Generated files 22 | .idea/**/contentModel.xml 23 | 24 | # Sensitive or high-churn files 25 | .idea/**/dataSources/ 26 | .idea/**/dataSources.ids 27 | .idea/**/dataSources.local.xml 28 | .idea/**/sqlDataSources.xml 29 | .idea/**/dynamic.xml 30 | .idea/**/uiDesigner.xml 31 | .idea/**/dbnavigator.xml 32 | 33 | # Gradle 34 | .idea/**/gradle.xml 35 | .idea/**/libraries 36 | 37 | # Gradle and Maven with auto-import 38 | # When using Gradle or Maven with auto-import, you should exclude module files, 39 | # since they will be recreated, and may cause churn. Uncomment if using 40 | # auto-import. 41 | .idea/modules.xml 42 | .idea/*.iml 43 | .idea/modules 44 | *.iml 45 | *.ipr 46 | 47 | # CMake 48 | cmake-build-*/ 49 | 50 | # Mongo Explorer plugin 51 | .idea/**/mongoSettings.xml 52 | 53 | # File-based project format 54 | *.iws 55 | 56 | # IntelliJ 57 | out/ 58 | 59 | # mpeltonen/sbt-idea plugin 60 | .idea_modules/ 61 | 62 | # JIRA plugin 63 | atlassian-ide-plugin.xml 64 | 65 | # Cursive Clojure plugin 66 | .idea/replstate.xml 67 | 68 | # Crashlytics plugin (for Android Studio and IntelliJ) 69 | com_crashlytics_export_strings.xml 70 | crashlytics.properties 71 | crashlytics-build.properties 72 | fabric.properties 73 | 74 | # Editor-based Rest Client 75 | .idea/httpRequests 76 | 77 | # Android studio 3.1+ serialized cache file 78 | .idea/caches/build_file_checksums.ser 79 | 80 | ### Intellij Patch ### 81 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 82 | 83 | # *.iml 84 | # modules.xml 85 | # .idea/misc.xml 86 | # *.ipr 87 | 88 | # Sonarlint plugin 89 | .idea/**/sonarlint/ 90 | 91 | # SonarQube Plugin 92 | .idea/**/sonarIssues.xml 93 | 94 | # Markdown Navigator plugin 95 | .idea/**/markdown-navigator.xml 96 | .idea/**/markdown-navigator/ 97 | 98 | ### Node ### 99 | # Logs 100 | logs 101 | *.log 102 | npm-debug.log* 103 | yarn-debug.log* 104 | yarn-error.log* 105 | lerna-debug.log* 106 | 107 | # Diagnostic reports (https://nodejs.org/api/report.html) 108 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 109 | 110 | # Runtime data 111 | pids 112 | *.pid 113 | *.seed 114 | *.pid.lock 115 | 116 | # Directory for instrumented libs generated by jscoverage/JSCover 117 | lib-cov 118 | 119 | # Coverage directory used by tools like istanbul 120 | coverage 121 | *.lcov 122 | 123 | # nyc test coverage 124 | .nyc_output 125 | 126 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 127 | .grunt 128 | 129 | # Bower dependency directory (https://bower.io/) 130 | bower_components 131 | 132 | # node-waf configuration 133 | .lock-wscript 134 | 135 | # Compiled binary addons (https://nodejs.org/api/addons.html) 136 | build/Release 137 | 138 | # Dependency directories 139 | node_modules/ 140 | jspm_packages/ 141 | 142 | # TypeScript v1 declaration files 143 | typings/ 144 | 145 | # TypeScript cache 146 | *.tsbuildinfo 147 | 148 | # Optional npm cache directory 149 | .npm 150 | 151 | # Optional eslint cache 152 | .eslintcache 153 | 154 | # Optional REPL history 155 | .node_repl_history 156 | 157 | # Output of 'npm pack' 158 | *.tgz 159 | 160 | # Yarn Integrity file 161 | .yarn-integrity 162 | 163 | # dotenv environment variables file 164 | .env 165 | .env.test 166 | 167 | # parcel-bundler cache (https://parceljs.org/) 168 | .cache 169 | 170 | # next.js build output 171 | .next 172 | 173 | # nuxt.js build output 174 | .nuxt 175 | 176 | # rollup.js default build output 177 | dist/ 178 | 179 | # generated types 180 | types/ 181 | 182 | # Uncomment the public line if your project uses Gatsby 183 | # https://nextjs.org/blog/next-9-1#public-directory-support 184 | # https://create-react-app.dev/docs/using-the-public-folder/#docsNav 185 | # public 186 | 187 | # Storybook build outputs 188 | .out 189 | .storybook-out 190 | 191 | # vuepress build output 192 | .vuepress/dist 193 | 194 | # Serverless directories 195 | .serverless/ 196 | 197 | # FuseBox cache 198 | .fusebox/ 199 | 200 | # DynamoDB Local files 201 | .dynamodb/ 202 | 203 | # Temporary folders 204 | tmp/ 205 | temp/ 206 | 207 | # End of https://www.gitignore.io/api/node,intellij 208 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "11" 4 | 5 | stages: 6 | - build 7 | - test 8 | - verify 9 | 10 | install: 11 | - yarn install 12 | 13 | matrix: 14 | include: 15 | - name: build 16 | stage: build 17 | script: 18 | - yarn run build 19 | 20 | - name: lint 21 | stage: verify 22 | script: 23 | - yarn run lint 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2020 Bas van Meurs, Robbin Baauw, Planning.nl 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vugel 2 | 3 | [![Build Status](https://img.shields.io/travis/Planning-nl/vugel/master.svg)](https://travis-ci.org/Planning-nl/vugel) 4 | [![NPM Version](https://img.shields.io/npm/v/vugel)](https://www.npmjs.com/package/vugel) 5 | 6 | > Fast Vue3 renderer powered by WebGL 7 | 8 | - [Documentation](https://vugel.planning.nl) 9 | - [Example](https://vugel-example.planning.nl) 10 | - [Example source](https://github.com/Planning-nl/vugel-example) 11 | - [Codepen example](https://codepen.io/basvanmeurs/pen/vYNGRGP?editors=1010) 12 | 13 | ## License 14 | 15 | [Apache](https://opensource.org/licenses/Apache-2.0) 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vugel", 3 | "description": "Vue3 webgl renderer", 4 | "version": "1.0.20", 5 | "author": "Bas van Meurs, Robbin Baauw, Planning.nl", 6 | "email": "bas@planning.nl", 7 | "bugs": { 8 | "url": "https://github.com/Planning-nl/vugel/issues", 9 | "email": "support@planning.nl" 10 | }, 11 | "dependencies": { 12 | "tree2d": "^1.0.14" 13 | }, 14 | "devDependencies": { 15 | "@planning.nl/eslint-config": "^1.0.6", 16 | "@planning.nl/webpack-config": "^1.0.4", 17 | "eslint": "^6.8.0", 18 | "ts-loader": "^6.2.2", 19 | "typescript": "3.8.3", 20 | "vue": "^3.0.0-rc.2" 21 | }, 22 | "files": [ 23 | "esm/**", 24 | "cjs/**", 25 | "dist/**", 26 | "src/**", 27 | "types/**" 28 | ], 29 | "homepage": "https://github.com/Planning-nl/vugel#readme", 30 | "keywords": [], 31 | "license": "apache", 32 | "main": "cjs/index.js", 33 | "module": "esm/index.js", 34 | "jsdelivr": "dist/vugel.js", 35 | "unpkg": "dist/vugel.js", 36 | "peerDependencies": { 37 | "vue": "^3.0.0-rc.2" 38 | }, 39 | "repository": "github:Planning-nl/vugel", 40 | "scripts": { 41 | "build": "npm run build:cjs & npm run build:esm & npm run build:umd", 42 | "watch:cjs": "tsc --watch --module commonjs --outDir cjs", 43 | "build:cjs": "tsc --module commonjs --outDir cjs", 44 | "watch:esm": "tsc --watch --module es2015 --outDir esm", 45 | "build:esm": "tsc --module es2015 --outDir esm", 46 | "build:umd": "NODE_ENV=production webpack --progress --display-error-details", 47 | "lint": "eslint -c .eslintrc.js 'src/**/*.ts'", 48 | "lint:fix": "eslint --fix -c .eslintrc.js **/*.ts", 49 | "prepare": "npm run build", 50 | "prepublish": "npm run build", 51 | "version": "npm run lint" 52 | }, 53 | "types": "types/index.d.ts", 54 | "engines": { 55 | "node": ">=8.0.0" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/compiler/compile.ts: -------------------------------------------------------------------------------- 1 | import { baseCompile, baseParse, CompilerOptions, CodegenResult, ParserOptions, RootNode } from "@vue/compiler-core"; 2 | 3 | import { parserOptionsMinimal } from "./parserOptionsMinimal"; 4 | 5 | const parserOptions = parserOptionsMinimal; 6 | 7 | export function compile(template: string, options: CompilerOptions = {}): CodegenResult { 8 | return baseCompile(template, { 9 | ...parserOptions, 10 | ...options, 11 | nodeTransforms: [...(options.nodeTransforms || [])], 12 | directiveTransforms: { 13 | ...(options.directiveTransforms || {}), 14 | }, 15 | }); 16 | } 17 | 18 | export function parse(template: string, options: ParserOptions = {}): RootNode { 19 | return baseParse(template, { 20 | ...parserOptions, 21 | ...options, 22 | }); 23 | } 24 | -------------------------------------------------------------------------------- /src/compiler/compileVugel.ts: -------------------------------------------------------------------------------- 1 | import { RenderFunction, warn } from "@vue/runtime-core"; 2 | import * as runtimeVue from "@vue/runtime-core"; 3 | import { CompilerError, CompilerOptions } from "@vue/compiler-core"; 4 | import { compile } from "./compile"; 5 | import * as runtimeVugel from "../runtime"; 6 | 7 | const compileCache: Record = Object.create(null); 8 | 9 | export function compileVugel(template: string | HTMLElement, options?: CompilerOptions): RenderFunction { 10 | if (!(typeof template === "string")) { 11 | if (template.nodeType) { 12 | template = template.innerHTML; 13 | } else { 14 | warn(`invalid template option: `, template); 15 | return () => { 16 | // Noop 17 | }; 18 | } 19 | } 20 | 21 | const key = template; 22 | const cached = compileCache[key]; 23 | if (cached) { 24 | return cached; 25 | } 26 | 27 | if (template[0] === "#") { 28 | const el = document.querySelector(template); 29 | if (!el) { 30 | warn(`Template element not found or is empty: ${template}`); 31 | } 32 | template = el ? el.innerHTML : ``; 33 | } 34 | 35 | const { code } = compile(template, { 36 | hoistStatic: true, 37 | onError(err: CompilerError) { 38 | const message = `Template compilation error: ${err.message}`; 39 | const codeFrame = err.loc; 40 | warn(codeFrame ? `${message}\n${codeFrame}` : message); 41 | }, 42 | ...options, 43 | }); 44 | 45 | const render = new Function("Vue", code)({ 46 | ...runtimeVugel, 47 | ...runtimeVue, 48 | }) as RenderFunction; 49 | return (compileCache[key] = render); 50 | } 51 | -------------------------------------------------------------------------------- /src/compiler/makeMap.ts: -------------------------------------------------------------------------------- 1 | // Make a map and return a function for checking if a key 2 | // is in that map. 3 | export function makeMap(str: string, expectsLowerCase?: boolean): (key: string) => boolean { 4 | const map: Record = Object.create(null); 5 | const list: Array = str.split(","); 6 | for (let i = 0; i < list.length; i++) { 7 | map[list[i]] = true; 8 | } 9 | return expectsLowerCase ? (val) => !!map[val.toLowerCase()] : (val) => !!map[val]; 10 | } 11 | -------------------------------------------------------------------------------- /src/compiler/parserOptionsMinimal.ts: -------------------------------------------------------------------------------- 1 | import { ParserOptions } from "@vue/compiler-core"; 2 | import { makeMap } from "./makeMap"; 3 | 4 | export const parserOptionsMinimal: ParserOptions = { 5 | isVoidTag: () => false, 6 | isNativeTag: (tag) => { 7 | return isTree2dTag(tag); 8 | }, 9 | isPreTag: () => false, 10 | }; 11 | 12 | const TREE2D_TAGS = 13 | "container,direct-container,rectangle,picture,text,paragraph,styled-rectangle,drawing,texture,svg,grayscale,rounded,shader,box-blur"; 14 | 15 | export const isTree2dTag = makeMap(TREE2D_TAGS); 16 | -------------------------------------------------------------------------------- /src/events/focusEvents.ts: -------------------------------------------------------------------------------- 1 | import { Node } from "../runtime/nodes/Node"; 2 | import { getCommonAncestor, getCurrentContext } from "./utils"; 3 | import { RegisterEventDispatcher, VugelEvent } from "./types"; 4 | import { VugelStage } from "../wrapper"; 5 | 6 | export class FocusEvents { 7 | private focusedNode?: Node = undefined; 8 | private updatingFocusPath: boolean = false; 9 | 10 | constructor(private targetElement: HTMLElement, private stage: VugelStage) { 11 | this.ensureCanvasFocusable(); 12 | this.targetElement.addEventListener("click", (e) => this.onCanvasClick(e)); 13 | this.targetElement.addEventListener("blur", (e) => this.onCanvasBlur(e)); 14 | } 15 | 16 | getFocusedNode(): Node | undefined { 17 | return this.focusedNode; 18 | } 19 | 20 | private ensureCanvasFocusable() { 21 | if (!this.targetElement.hasAttribute("tabindex")) { 22 | this.targetElement.setAttribute("tabindex", "-1"); 23 | } 24 | } 25 | 26 | private onCanvasClick(e: MouseEvent) { 27 | // Automatically focus on clicked elements. 28 | const { currentElement } = getCurrentContext(e, this.stage); 29 | const node = currentElement?.element.data; 30 | this.setFocus(node); 31 | } 32 | 33 | private onCanvasBlur(e: FocusEvent) { 34 | this.setFocus(undefined); 35 | } 36 | 37 | public setFocus(focused: Node | undefined) { 38 | if (this.updatingFocusPath) { 39 | console.warn( 40 | "It's not allowed to focus from within a focus-related event. Use setInterval to schedule a focus change.", 41 | ); 42 | } else if (this.focusedNode !== focused) { 43 | this.updateFocusPath(focused); 44 | } 45 | } 46 | 47 | public updateFocusPath(newFocusedNode: Node | undefined) { 48 | this.updatingFocusPath = true; 49 | 50 | const prevFocusedNode = this.focusedNode; 51 | const commonAncestor = getCommonAncestor(this.focusedNode, newFocusedNode); 52 | 53 | this.focusedNode = newFocusedNode; 54 | 55 | // Use event order as specified in https://www.w3.org/TR/DOM-Level-3-Events/#events-focusevent-event-order 56 | 57 | if (prevFocusedNode) { 58 | prevFocusedNode.dispatchBubbledEvent( 59 | this.createFocusEvent("focusout", prevFocusedNode, newFocusedNode), 60 | commonAncestor, 61 | ); 62 | } 63 | 64 | if (newFocusedNode) { 65 | newFocusedNode.dispatchBubbledEvent( 66 | this.createFocusEvent("focusin", newFocusedNode, prevFocusedNode), 67 | commonAncestor, 68 | ); 69 | } 70 | 71 | if (prevFocusedNode) { 72 | prevFocusedNode.dispatchEvent(this.createFocusEvent("blur", prevFocusedNode, newFocusedNode)); 73 | } 74 | 75 | if (newFocusedNode) { 76 | newFocusedNode.dispatchEvent(this.createFocusEvent("focus", newFocusedNode, prevFocusedNode)); 77 | } 78 | 79 | this.updatingFocusPath = false; 80 | } 81 | 82 | private createFocusEvent( 83 | type: SupportedFocusEvents, 84 | target: Node | undefined, 85 | relatedTarget: Node | undefined, 86 | ): VugelFocusEvent { 87 | return { 88 | cancelBubble: false, 89 | 90 | // Event 91 | type, 92 | 93 | relatedTarget: relatedTarget ?? null, 94 | target: target ?? null, 95 | currentTarget: null, 96 | 97 | originalEvent: undefined, 98 | }; 99 | } 100 | } 101 | 102 | export interface VugelFocusEvent extends VugelEvent { 103 | relatedTarget: Node | null; 104 | } 105 | 106 | export type SupportedFocusEvents = keyof Pick; 107 | 108 | export const focusEventTranslator: { 109 | [x in SupportedFocusEvents]: "onFocusin" | "onFocusout" | "onFocus" | "onBlur"; 110 | } = { 111 | focusin: "onFocusin", 112 | focusout: "onFocusout", 113 | focus: "onFocus", 114 | blur: "onBlur", 115 | } as const; 116 | 117 | export const setupFocusEvents: RegisterEventDispatcher = (targetElement: HTMLElement, stage: VugelStage) => { 118 | return new FocusEvents(targetElement, stage); 119 | }; 120 | -------------------------------------------------------------------------------- /src/events/hover.ts: -------------------------------------------------------------------------------- 1 | import { translateEvent } from "./mouseEvents"; 2 | import { VugelStage } from "../wrapper"; 3 | import { Node } from "../runtime/nodes"; 4 | 5 | export const setupHover = (targetElement: HTMLElement, stage: VugelStage) => { 6 | let currentHoveredTarget: Node | null = null; 7 | 8 | function handleNewHoveredNode(lastHoveredTarget: Node | null, newHoveredTarget: Node | null) { 9 | if (newHoveredTarget) { 10 | const cursorType = getCursorType(newHoveredTarget); 11 | targetElement.style.cursor = cursorType || ""; 12 | } 13 | } 14 | 15 | function getCursorType(node: Node): string | undefined { 16 | if (node["cursor-type"]) { 17 | return node["cursor-type"]; 18 | } else { 19 | const parent = node.getParentNode(); 20 | if (parent) { 21 | return getCursorType(parent); 22 | } else { 23 | return undefined; 24 | } 25 | } 26 | } 27 | 28 | targetElement.addEventListener("mousemove", (e: MouseEvent) => { 29 | const translatedEvent = translateEvent(stage, e); 30 | const newHoveredTarget = translatedEvent.target; 31 | if (newHoveredTarget !== currentHoveredTarget) { 32 | const lastHoveredTarget = currentHoveredTarget; 33 | currentHoveredTarget = newHoveredTarget; 34 | handleNewHoveredNode(lastHoveredTarget, newHoveredTarget); 35 | } 36 | }); 37 | }; 38 | -------------------------------------------------------------------------------- /src/events/index.ts: -------------------------------------------------------------------------------- 1 | import { VugelStage } from "../wrapper"; 2 | import { setupMouseEvents } from "./mouseEvents"; 3 | import { setupTouchEvents } from "./touchEvents"; 4 | import { FocusEvents, setupFocusEvents } from "./focusEvents"; 5 | import { setupKeyboardEvents } from "./keyboardEvents"; 6 | import { setupHover } from "./hover"; 7 | 8 | export type EventHelpers = { 9 | focusManager: FocusEvents; 10 | }; 11 | 12 | export const setupEvents = (targetElement: HTMLElement, stage: VugelStage): EventHelpers => { 13 | setupMouseEvents(targetElement, stage); 14 | setupTouchEvents(targetElement, stage); 15 | const focusManager = setupFocusEvents(targetElement, stage); 16 | setupKeyboardEvents(targetElement, stage); 17 | setupHover(targetElement, stage); 18 | return { focusManager }; 19 | }; 20 | 21 | export * from "./types"; 22 | export * from "./mouseEvents"; 23 | export * from "./touchEvents"; 24 | export * from "./focusEvents"; 25 | export * from "./keyboardEvents"; 26 | export * from "./hover"; 27 | -------------------------------------------------------------------------------- /src/events/keyboardEvents.ts: -------------------------------------------------------------------------------- 1 | import { EventTranslator, RegisterEventDispatcher, VueEventsOfType, VugelEvent } from "./index"; 2 | import { Node } from "../runtime/nodes"; 3 | import { VugelStage } from "../wrapper"; 4 | 5 | export interface VugelKeyboardEvent extends VugelEvent {} 6 | 7 | const translateEvent: EventTranslator = (stage, e) => { 8 | let currentNode: Node | undefined = stage.eventHelpers.focusManager.getFocusedNode(); 9 | if (!currentNode) { 10 | currentNode = stage.root.data; 11 | } 12 | 13 | return { 14 | cancelBubble: false, 15 | 16 | // Event 17 | type: e.type as SupportedKeyboardEvents, 18 | currentTarget: currentNode ?? null, 19 | target: currentNode ?? null, 20 | 21 | originalEvent: e, 22 | }; 23 | }; 24 | 25 | // https://www.w3.org/TR/uievents/#events-keyboardevents 26 | const dispatchKeyboardEvent = (stage: VugelStage) => { 27 | return (e: KeyboardEvent) => { 28 | const translatedEvent = translateEvent(stage, e); 29 | if (translatedEvent) { 30 | dispatchVugelKeyboardEvent(translatedEvent); 31 | } 32 | }; 33 | }; 34 | 35 | export const dispatchVugelKeyboardEvent = (translatedEvent: VugelKeyboardEvent) => { 36 | translatedEvent.target?.dispatchBubbledEvent(translatedEvent); 37 | }; 38 | 39 | export type SupportedKeyboardEvents = keyof Pick; 40 | 41 | export const keyboardEventTranslator: { 42 | [x in SupportedKeyboardEvents]: VueEventsOfType; 43 | } = { 44 | keypress: "onKeypress", 45 | keydown: "onKeydown", 46 | keyup: "onKeyup", 47 | } as const; 48 | 49 | export const setupKeyboardEvents: RegisterEventDispatcher = (targetElement, stage) => { 50 | for (const key in keyboardEventTranslator) { 51 | targetElement.addEventListener(key, dispatchKeyboardEvent(stage) as EventListener); 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /src/events/mouseEvents.ts: -------------------------------------------------------------------------------- 1 | import { Node } from "../runtime/nodes"; 2 | import { EventTranslator, RegisterEventDispatcher, VueEventsOfType, VugelEvent } from "./index"; 3 | import { getCommonAncestor, getCurrentContext } from "./utils"; 4 | import { VugelStage } from "../wrapper"; 5 | import { ElementCoordinatesInfo } from "tree2d"; 6 | 7 | export interface VugelMouseEvent extends VugelEvent { 8 | readonly canvasOffsetX: number; 9 | readonly canvasOffsetY: number; 10 | readonly elementOffsetX: number; 11 | readonly elementOffsetY: number; 12 | readonly currentElement: ElementCoordinatesInfo | undefined; 13 | } 14 | 15 | export type MouseEventState = { 16 | activeNode?: Node; 17 | }; 18 | 19 | export const translateEvent: EventTranslator = (stage, e) => { 20 | const { currentElement, canvasOffsetX, canvasOffsetY } = getCurrentContext(e, stage); 21 | const currentNode = currentElement?.element.data; 22 | 23 | return { 24 | cancelBubble: false, 25 | 26 | // Event 27 | type: e.type as SupportedMouseEvents, 28 | currentTarget: currentNode ?? null, 29 | target: currentNode ?? null, 30 | 31 | // MouseEvent 32 | canvasOffsetX: canvasOffsetX, 33 | canvasOffsetY: canvasOffsetY, 34 | elementOffsetX: currentElement?.offsetX ?? 0, 35 | elementOffsetY: currentElement?.offsetY ?? 0, 36 | currentElement: currentElement, 37 | 38 | originalEvent: e, 39 | }; 40 | }; 41 | 42 | // https://www.w3.org/TR/uievents/#events-mouse-types 43 | const dispatchMouseEvent = (stage: VugelStage, eventState: MouseEventState) => { 44 | return (e: MouseEvent) => { 45 | const translatedEvent = translateEvent(stage, e); 46 | dispatchVugelMouseEvent(translatedEvent, eventState); 47 | 48 | // Prevent selecting nearby text when double-clicking on the canvas. 49 | e.preventDefault(); 50 | }; 51 | }; 52 | 53 | export const dispatchVugelMouseEvent = (translatedEvent: VugelMouseEvent, eventState: MouseEventState) => { 54 | const prevNode = eventState.activeNode; 55 | const currentNode = translatedEvent.currentElement?.element.data; 56 | 57 | switch (translatedEvent.type as SupportedMouseEvents) { 58 | case "auxclick": 59 | case "click": 60 | case "contextmenu": 61 | case "dblclick": 62 | case "mousedown": 63 | case "mouseup": { 64 | currentNode?.dispatchBubbledEvent(translatedEvent); 65 | break; 66 | } 67 | case "mouseenter": { 68 | eventState.activeNode = undefined; 69 | 70 | if (currentNode) { 71 | eventState.activeNode = currentNode; 72 | currentNode.dispatchBubbledEvent(translatedEvent, true); 73 | } 74 | 75 | break; 76 | } 77 | case "mouseover": { 78 | eventState.activeNode = undefined; 79 | 80 | if (currentNode) { 81 | eventState.activeNode = currentNode; 82 | currentNode?.dispatchBubbledEvent(translatedEvent); 83 | } 84 | 85 | break; 86 | } 87 | case "mouseleave": { 88 | prevNode?.dispatchBubbledEvent(translatedEvent, true); 89 | break; 90 | } 91 | case "mouseout": { 92 | prevNode?.dispatchBubbledEvent({ 93 | ...translatedEvent, 94 | target: prevNode, 95 | }); 96 | break; 97 | } 98 | case "mousemove": { 99 | if (currentNode !== undefined && prevNode != currentNode) { 100 | const commonAncestor = getCommonAncestor(prevNode, currentNode); 101 | 102 | prevNode?.dispatchBubbledEvent({ 103 | ...translatedEvent, 104 | type: "mouseout", 105 | target: prevNode, 106 | }); 107 | 108 | prevNode?.dispatchBubbledEvent( 109 | { 110 | ...translatedEvent, 111 | type: "mouseleave", 112 | target: prevNode, 113 | }, 114 | commonAncestor, 115 | ); 116 | 117 | currentNode.dispatchBubbledEvent({ 118 | ...translatedEvent, 119 | type: "mouseover", 120 | }); 121 | 122 | currentNode.dispatchBubbledEvent( 123 | { 124 | ...translatedEvent, 125 | type: "mouseenter", 126 | }, 127 | commonAncestor, 128 | ); 129 | } 130 | 131 | // Mousemove 132 | currentNode?.dispatchBubbledEvent(translatedEvent); 133 | 134 | eventState.activeNode = currentNode; 135 | } 136 | } 137 | }; 138 | 139 | export type SupportedMouseEvents = keyof Pick< 140 | GlobalEventHandlersEventMap, 141 | | "auxclick" 142 | | "click" 143 | | "contextmenu" 144 | | "dblclick" 145 | | "mousedown" 146 | | "mouseenter" 147 | | "mouseleave" 148 | | "mousemove" 149 | | "mouseout" 150 | | "mouseover" 151 | | "mouseup" 152 | >; 153 | 154 | export const mouseEventTranslator: { 155 | [x in SupportedMouseEvents]: VueEventsOfType; 156 | } = { 157 | auxclick: "onAuxclick", 158 | click: "onClick", 159 | contextmenu: "onContextmenu", 160 | dblclick: "onDblclick", 161 | mousedown: "onMousedown", 162 | mouseenter: "onMouseenter", 163 | mouseleave: "onMouseleave", 164 | mousemove: "onMousemove", 165 | mouseout: "onMouseout", 166 | mouseover: "onMouseover", 167 | mouseup: "onMouseup", 168 | } as const; 169 | 170 | export const setupMouseEvents: RegisterEventDispatcher = (targetElement, stage) => { 171 | const eventState: MouseEventState = {}; 172 | 173 | for (const key in mouseEventTranslator) { 174 | targetElement.addEventListener(key, dispatchMouseEvent(stage, eventState) as EventListener); 175 | } 176 | }; 177 | -------------------------------------------------------------------------------- /src/events/touchEvents.ts: -------------------------------------------------------------------------------- 1 | import { 2 | dispatchVugelMouseEvent, 3 | EventTranslator, 4 | MouseEventState, 5 | RegisterEventDispatcher, 6 | SupportedMouseEvents, 7 | VueEventsOfType, 8 | VugelMouseEvent, 9 | } from "./index"; 10 | import { getCurrentContext } from "./utils"; 11 | import { VugelStage } from "../wrapper"; 12 | 13 | const translateEvent: EventTranslator = (stage, e) => { 14 | let currentTouch: Touch; 15 | 16 | const eventType = e.type as SupportedTouchEvents; 17 | if (eventType === "touchend" || eventType === "touchcancel") { 18 | currentTouch = e.changedTouches[0]; 19 | } else { 20 | currentTouch = e.touches[0]; 21 | } 22 | 23 | const { currentElement, canvasOffsetX, canvasOffsetY } = getCurrentContext(currentTouch, stage); 24 | const currentNode = currentElement?.element.data; 25 | 26 | return { 27 | cancelBubble: false, 28 | 29 | // Event 30 | type: e.type as SupportedMouseEvents, 31 | currentTarget: currentNode ?? null, 32 | target: currentNode ?? null, 33 | 34 | // MouseEvent 35 | canvasOffsetX: canvasOffsetX, 36 | canvasOffsetY: canvasOffsetY, 37 | elementOffsetX: currentElement?.offsetX ?? 0, 38 | elementOffsetY: currentElement?.offsetY ?? 0, 39 | 40 | originalEvent: e, 41 | currentElement: currentElement, 42 | }; 43 | }; 44 | 45 | // https://www.w3.org/TR/touch-events/#list-of-touchevent-types 46 | const dispatchTouchEvent = (stage: VugelStage, eventState: MouseEventState) => { 47 | return (e: TouchEvent) => { 48 | const translatedEvent = translateEvent(stage, e); 49 | let correspondingMouseEvent: SupportedMouseEvents; 50 | 51 | switch (e.type as SupportedTouchEvents) { 52 | case "touchstart": 53 | correspondingMouseEvent = "mousedown"; 54 | break; 55 | case "touchend": 56 | case "touchcancel": 57 | correspondingMouseEvent = "mouseup"; 58 | break; 59 | case "touchmove": 60 | correspondingMouseEvent = "mousemove"; 61 | break; 62 | } 63 | 64 | const translatedMouseEvent: VugelMouseEvent = { 65 | ...translatedEvent, 66 | type: correspondingMouseEvent, 67 | currentElement: translatedEvent.currentElement, 68 | }; 69 | 70 | dispatchVugelMouseEvent(translatedMouseEvent, eventState); 71 | }; 72 | }; 73 | 74 | export type SupportedTouchEvents = keyof Pick< 75 | GlobalEventHandlersEventMap, 76 | "touchcancel" | "touchend" | "touchstart" | "touchmove" 77 | >; 78 | 79 | export const touchEventTranslator: { 80 | [x in SupportedTouchEvents]: VueEventsOfType; 81 | } = { 82 | touchcancel: "onTouchcancel", 83 | touchend: "onTouchend", 84 | touchmove: "onTouchmove", 85 | touchstart: "onTouchstart", 86 | } as const; 87 | 88 | export const setupTouchEvents: RegisterEventDispatcher = (targetElement, stage) => { 89 | const eventState: MouseEventState = {}; 90 | 91 | for (const key in touchEventTranslator) { 92 | targetElement.addEventListener(key, dispatchTouchEvent(stage, eventState) as EventListener); 93 | } 94 | }; 95 | -------------------------------------------------------------------------------- /src/events/types.ts: -------------------------------------------------------------------------------- 1 | import { mouseEventTranslator, SupportedMouseEvents } from "./mouseEvents"; 2 | import { Node } from "../runtime/nodes/Node"; 3 | import { Events } from "@vue/runtime-dom"; 4 | import { SupportedTouchEvents, touchEventTranslator } from "./touchEvents"; 5 | import { VugelStage } from "../wrapper"; 6 | import { focusEventTranslator, SupportedFocusEvents } from "./focusEvents"; 7 | import { keyboardEventTranslator, SupportedKeyboardEvents } from "./keyboardEvents"; 8 | 9 | export type SupportedEvents = 10 | | SupportedMouseEvents 11 | | SupportedTouchEvents 12 | | SupportedFocusEvents 13 | | SupportedKeyboardEvents; 14 | 15 | export interface VugelEvent { 16 | cancelBubble: boolean; 17 | readonly currentTarget: Node | null; 18 | readonly target: Node | null; 19 | readonly type: SupportedEvents; 20 | readonly originalEvent: T; 21 | } 22 | 23 | export const eventTranslators = { 24 | ...mouseEventTranslator, 25 | ...touchEventTranslator, 26 | ...focusEventTranslator, 27 | ...keyboardEventTranslator, 28 | } as const; 29 | 30 | // Type helpers 31 | export type RegisterEventDispatcher = (targetElement: HTMLElement, stage: VugelStage) => any; 32 | 33 | export type EventTranslator> = (stage: VugelStage, event: O) => V; 34 | 35 | export type VugelEventListener> = (event: T) => any; 36 | 37 | export type VueEventsOfType = keyof Pick< 38 | Events, 39 | { 40 | [K in keyof Events]: Events[K] extends T ? (T extends Events[K] ? K : never) : never; 41 | }[keyof Events] 42 | >; 43 | -------------------------------------------------------------------------------- /src/events/utils.ts: -------------------------------------------------------------------------------- 1 | import { ElementCoordinatesInfo } from "tree2d"; 2 | import { Node } from "../runtime/nodes/Node"; 3 | import { Base } from "../runtime/nodes/Base"; 4 | import { VugelStage } from "../wrapper"; 5 | 6 | export function getCanvasOffset(e: PageCoordinates, stage: VugelStage): { x: number; y: number } { 7 | const rect = stage.getCanvas().getBoundingClientRect(); 8 | return { x: e.pageX - (rect.left + window.scrollX), y: e.pageY - (rect.top + window.scrollY) }; 9 | } 10 | 11 | export function getCurrentContext( 12 | e: PageCoordinates, 13 | stage: VugelStage, 14 | ): { 15 | currentElement: ElementCoordinatesInfo | undefined; 16 | canvasOffsetX: number; 17 | canvasOffsetY: number; 18 | } { 19 | const { x: canvasX, y: canvasY } = getCanvasOffset(e, stage); 20 | 21 | const elementsAtCanvasCoordinates = stage.getElementsAtCoordinates(canvasX, canvasY); 22 | return { 23 | currentElement: elementsAtCanvasCoordinates.find((v) => v.element.data?.capturePointerEvents()), 24 | canvasOffsetX: canvasX, 25 | canvasOffsetY: canvasY, 26 | }; 27 | } 28 | 29 | export function getAncestors(node: Node): Base[] { 30 | const path = []; 31 | let n: Base | undefined = node; 32 | do { 33 | path.push(n); 34 | n = n.parent; 35 | } while (n !== undefined); 36 | return path.reverse(); 37 | } 38 | 39 | export function getCommonAncestor(node1: Node | undefined, node2: Node | undefined): Base | undefined { 40 | if (!node1 || !node2) { 41 | return undefined; 42 | } 43 | 44 | const pathNode1 = getAncestors(node1); 45 | const pathNode2 = getAncestors(node2); 46 | 47 | const m = Math.min(pathNode1.length, pathNode2.length); 48 | let index; 49 | for (index = 0; index < m; index++) { 50 | if (pathNode1[index] !== pathNode2[index]) { 51 | break; 52 | } 53 | } 54 | 55 | return index > 0 ? pathNode1[index - 1] : undefined; 56 | } 57 | 58 | type PageCoordinates = { pageX: number; pageY: number }; 59 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export { compileVugel } from "./compiler/compileVugel"; 2 | 3 | export { Vugel, VugelStage } from "./wrapper"; 4 | export { VugelEventListener, VugelMouseEvent, VugelEvent } from "./events"; 5 | 6 | export { compile, parse } from "./compiler/compile"; 7 | 8 | export * from "./runtime/index"; 9 | -------------------------------------------------------------------------------- /src/runtime/index.ts: -------------------------------------------------------------------------------- 1 | import { createRenderer, RootRenderFunction } from "@vue/runtime-core"; 2 | import { nodeOps } from "./nodeOps"; 3 | import { patchProp } from "./patchProp"; 4 | import { VugelStage } from "../wrapper"; 5 | 6 | export function createRendererForStage(stage: VugelStage): RootRenderFunction { 7 | const { render } = createRenderer({ 8 | patchProp, 9 | ...nodeOps(stage), 10 | }); 11 | 12 | return render; 13 | } 14 | 15 | export * from "./nodes"; 16 | export * from "./utils"; 17 | -------------------------------------------------------------------------------- /src/runtime/nodeOps.ts: -------------------------------------------------------------------------------- 1 | import { types } from "./nodes/types"; 2 | import { Base } from "./nodes/Base"; 3 | import { Comment } from "./nodes/Comment"; 4 | import { RendererOptions } from "@vue/runtime-core"; 5 | import { TextNode } from "./nodes/TextNode"; 6 | import { VugelStage } from "../wrapper"; 7 | 8 | export const nodeOps = (stage: VugelStage): Omit, "patchProp"> => ({ 9 | insert: (child, parent, anchor) => { 10 | if (anchor != null) { 11 | parent._insertBefore(child, anchor); 12 | } else { 13 | parent._appendChild(child); 14 | } 15 | }, 16 | 17 | remove: (child) => { 18 | const parent = child.parent; 19 | if (parent != null) { 20 | parent._removeChild(child); 21 | } 22 | }, 23 | 24 | createElement: (tag: keyof typeof types, isSVG) => { 25 | let type = types[tag]; 26 | if (!type) { 27 | console.warn(`Unknown native tag: ${tag}`); 28 | type = types["container"]; 29 | } 30 | return new type(stage); 31 | }, 32 | 33 | createText: (text) => { 34 | return new TextNode(text); 35 | }, 36 | 37 | createComment: (text) => { 38 | return new Comment(text); 39 | }, 40 | 41 | setText: (node, text) => { 42 | // Noop 43 | }, 44 | 45 | setElementText: (node, text) => { 46 | node.setElementText(text); 47 | }, 48 | 49 | parentNode: (node) => (node.parent ? node.parent : null), 50 | 51 | nextSibling: (node) => node.nextSibling, 52 | 53 | querySelector: (selector) => { 54 | if (selector.startsWith("#")) { 55 | selector = selector.substr(1); 56 | } 57 | return stage.getById(selector)?.data || null; 58 | }, 59 | }); 60 | -------------------------------------------------------------------------------- /src/runtime/nodes/Base.ts: -------------------------------------------------------------------------------- 1 | import { Element } from "tree2d"; 2 | import { queuePostFlushCb } from "vue"; 3 | 4 | export class Base { 5 | public element?: Element = undefined; 6 | 7 | protected children = new Set(); 8 | 9 | public parent?: Base = undefined; 10 | 11 | public firstChild: Base | null = null; 12 | public lastChild: Base | null = null; 13 | public prevSibling: Base | null = null; 14 | public nextSibling: Base | null = null; 15 | 16 | private mustSync = false; 17 | 18 | constructor(element: Element | undefined) { 19 | this.element = element; 20 | } 21 | 22 | _appendChild(child: Base) { 23 | child.unlinkSiblings(); 24 | 25 | child.parent = this; 26 | this.children.add(child); 27 | 28 | if (!this.firstChild) { 29 | this.firstChild = child; 30 | } 31 | child.prevSibling = this.lastChild; 32 | child.nextSibling = null; 33 | if (this.lastChild) { 34 | this.lastChild.nextSibling = child; 35 | } 36 | this.lastChild = child; 37 | 38 | this.registerSync(); 39 | } 40 | 41 | private unlinkSiblings() { 42 | if (this.parent?.firstChild === this) { 43 | this.parent!.firstChild = this.nextSibling; 44 | } 45 | 46 | if (this.parent?.lastChild === this) { 47 | this.parent!.lastChild = this.prevSibling; 48 | } 49 | 50 | if (this.prevSibling) { 51 | this.prevSibling.nextSibling = this.nextSibling; 52 | } 53 | 54 | if (this.nextSibling) { 55 | this.nextSibling.prevSibling = this.prevSibling; 56 | } 57 | 58 | this.prevSibling = null; 59 | this.nextSibling = null; 60 | } 61 | 62 | _removeChild(child: Base) { 63 | child.unlinkSiblings(); 64 | 65 | child.parent = undefined; 66 | 67 | this.children.delete(child); 68 | 69 | this.registerSync(); 70 | } 71 | 72 | _insertBefore(child: Base, anchor: Base) { 73 | child.unlinkSiblings(); 74 | 75 | child.parent = this; 76 | 77 | if (anchor.prevSibling) { 78 | child.prevSibling = anchor.prevSibling; 79 | anchor.prevSibling.nextSibling = child; 80 | } 81 | 82 | anchor.prevSibling = child; 83 | child.nextSibling = anchor; 84 | 85 | if (this.firstChild === anchor) { 86 | this.firstChild = child; 87 | } 88 | 89 | this.children.add(child); 90 | 91 | this.registerSync(); 92 | } 93 | 94 | private registerSync() { 95 | if (!this.mustSync) { 96 | this.mustSync = true; 97 | registerUpdatedBase(this); 98 | } 99 | } 100 | 101 | syncWithTree2d() { 102 | // Ignore. 103 | this.mustSync = false; 104 | } 105 | 106 | setElementText(text: string) { 107 | // Default: ignore text. 108 | } 109 | } 110 | 111 | let pendingSyncBase: Base | undefined = undefined; 112 | 113 | const registerUpdatedBase = (base: Base) => { 114 | if (pendingSyncBase && pendingSyncBase !== base) { 115 | pendingSyncBase.syncWithTree2d(); 116 | } 117 | 118 | if (!pendingSyncBase) { 119 | queuePostFlushCb(() => { 120 | flushChanges(); 121 | }); 122 | } 123 | 124 | pendingSyncBase = base; 125 | }; 126 | 127 | const flushChanges = () => { 128 | if (pendingSyncBase) { 129 | pendingSyncBase.syncWithTree2d(); 130 | } 131 | pendingSyncBase = undefined; 132 | }; 133 | -------------------------------------------------------------------------------- /src/runtime/nodes/Comment.ts: -------------------------------------------------------------------------------- 1 | import { Base } from "./Base"; 2 | 3 | export class Comment extends Base { 4 | public readonly text: string; 5 | 6 | constructor(text: string) { 7 | super(undefined); 8 | this.text = text; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/runtime/nodes/Container.ts: -------------------------------------------------------------------------------- 1 | import { Node } from "./Node"; 2 | import { ensureBoolean, ensureFloat } from "../utils/TypeUtils"; 3 | import { Element } from "tree2d"; 4 | 5 | export class Container extends Node { 6 | public containerElement: Element = this.el; 7 | 8 | getChildren(): Node[] { 9 | let c = this.firstChild; 10 | const items: Node[] = []; 11 | while (c) { 12 | if (c.element) { 13 | items.push(c as Node); 14 | } 15 | c = c.nextSibling; 16 | } 17 | return items; 18 | } 19 | 20 | syncWithTree2d() { 21 | super.syncWithTree2d(); 22 | const items: Element[] = []; 23 | let c = this.firstChild; 24 | while (c) { 25 | if (c.element) { 26 | items.push(c.element); 27 | } 28 | c = c.nextSibling; 29 | } 30 | this.containerElement.childList.setItems(items); 31 | } 32 | 33 | set "force-z-index-context"(v: boolean) { 34 | this.el.forceZIndexContext = ensureBoolean(v); 35 | } 36 | 37 | get clipping() { 38 | return this.el.clipping; 39 | } 40 | 41 | set clipping(v: boolean) { 42 | this.el.clipping = ensureBoolean(v); 43 | } 44 | 45 | get clipbox() { 46 | return this.el.clipbox; 47 | } 48 | 49 | set clipbox(v: boolean) { 50 | this.el.clipbox = ensureBoolean(v); 51 | } 52 | 53 | get "render-to-texture"() { 54 | return this.el.renderToTexture; 55 | } 56 | 57 | set "render-to-texture"(v: boolean) { 58 | this.el.renderToTexture = ensureBoolean(v); 59 | } 60 | 61 | get "render-to-texture-lazy"() { 62 | return this.el.renderToTextureLazy; 63 | } 64 | 65 | set "render-to-texture-lazy"(v: boolean) { 66 | this.el.renderToTextureLazy = ensureBoolean(v); 67 | } 68 | 69 | get "render-to-texture-offscreen"() { 70 | return this.el.renderToTextureOffscreen; 71 | } 72 | 73 | set "render-to-texture-offscreen"(v: boolean) { 74 | this.el.renderToTextureOffscreen = ensureBoolean(v); 75 | } 76 | 77 | get "render-to-texture-colorize"() { 78 | return this.el.renderToTextureColorize; 79 | } 80 | 81 | set "render-to-texture-colorize"(v: boolean) { 82 | this.el.renderToTextureColorize = ensureBoolean(v); 83 | } 84 | 85 | get flex() { 86 | return this.el.flex; 87 | } 88 | 89 | set flex(v: boolean) { 90 | this.el.flex = ensureBoolean(v); 91 | } 92 | 93 | get "flex-direction"() { 94 | return this.el.flexDirection; 95 | } 96 | 97 | set "flex-direction"(v: "row" | "row-reverse" | "column" | "column-reverse") { 98 | this.el.flexDirection = v; 99 | } 100 | 101 | get "flex-wrap"() { 102 | return this.el.flexWrap; 103 | } 104 | 105 | set "flex-wrap"(v: boolean) { 106 | this.el.flexWrap = ensureBoolean(v); 107 | } 108 | 109 | get "flex-align-items"() { 110 | return this.el.flexAlignItems; 111 | } 112 | 113 | set "flex-align-items"(v: "flex-start" | "flex-end" | "center" | "stretch") { 114 | this.el.flexAlignItems = v; 115 | } 116 | 117 | get "flex-justify-content"() { 118 | return this.el.flexJustifyContent; 119 | } 120 | 121 | set "flex-justify-content"( 122 | v: "flex-start" | "flex-end" | "center" | "space-between" | "space-around" | "space-evenly", 123 | ) { 124 | this.el.flexJustifyContent = v; 125 | } 126 | 127 | get "flex-align-content"() { 128 | return this.el.flexAlignContent; 129 | } 130 | 131 | set "flex-align-content"( 132 | v: "flex-start" | "flex-end" | "center" | "space-between" | "space-around" | "space-evenly" | "stretch", 133 | ) { 134 | this.el.flexAlignContent = v; 135 | } 136 | 137 | get padding() { 138 | return this.el.padding; 139 | } 140 | 141 | set padding(v: number) { 142 | this.el.padding = ensureFloat(v); 143 | } 144 | 145 | get "padding-left"() { 146 | return this.el.paddingLeft; 147 | } 148 | 149 | set "padding-left"(v: number) { 150 | this.el.paddingLeft = ensureFloat(v); 151 | } 152 | 153 | get "padding-right"() { 154 | return this.el.paddingRight; 155 | } 156 | 157 | set "padding-right"(v: number) { 158 | this.el.paddingRight = ensureFloat(v); 159 | } 160 | 161 | get "padding-top"() { 162 | return this.el.paddingTop; 163 | } 164 | 165 | set "padding-top"(v: number) { 166 | this.el.paddingTop = ensureFloat(v); 167 | } 168 | 169 | get "padding-bottom"() { 170 | return this.el.paddingBottom; 171 | } 172 | 173 | set "padding-bottom"(v: number) { 174 | this.el.paddingBottom = ensureFloat(v); 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /src/runtime/nodes/DirectContainer.ts: -------------------------------------------------------------------------------- 1 | import { Container } from "./Container"; 2 | import { Node } from "./Node"; 3 | import { mixin } from "../utils/mixin"; 4 | import { Constructor } from "../utils/TypeUtils"; 5 | import { ConvertedDirectNode, DirectContainerMixin, getDirectType } from "./direct"; 6 | 7 | /** 8 | * Allows direct child mutations. 9 | * Disables vue vdom patching. 10 | */ 11 | export class DirectContainer extends Container { 12 | create(nodeType: Constructor): ConvertedDirectNode { 13 | const directType = getDirectType(nodeType); 14 | return new directType(this.stage) as any; 15 | } 16 | } 17 | 18 | mixin(DirectContainer, DirectContainerMixin); 19 | 20 | export interface DirectContainer extends DirectContainerMixin {} 21 | -------------------------------------------------------------------------------- /src/runtime/nodes/Node.ts: -------------------------------------------------------------------------------- 1 | import { Base } from "./Base"; 2 | import { Element, RelativeFunction, Stage } from "tree2d"; 3 | import { 4 | eventTranslators, 5 | SupportedEvents, 6 | VugelEvent, 7 | VugelEventListener, 8 | VugelFocusEvent, 9 | VugelKeyboardEvent, 10 | VugelMouseEvent, 11 | } from "../../events"; 12 | 13 | import { VugelStage } from "../../wrapper"; 14 | import { ensureBoolean, ensureColor, ensureFloat, isString } from "../utils/TypeUtils"; 15 | 16 | export { 17 | VugelNodeEventListener, 18 | VugelTextureErrorEventListener, 19 | VugelTextureEventListener, 20 | VugelResizeEventListener, 21 | VugelNodeEventObject, 22 | VugelTextureErrorEventObject, 23 | VugelTextureEventObject, 24 | VugelResizeEventObject, 25 | } from "./tree2dEvents"; 26 | 27 | import { 28 | VugelNodeEventListener, 29 | VugelTextureErrorEventListener, 30 | VugelTextureEventListener, 31 | VugelResizeEventListener, 32 | augmentTree2dElementEvent, 33 | } from "./tree2dEvents"; 34 | 35 | type NodeEvents = { 36 | onAuxclick?: VugelEventListener; 37 | onClick?: VugelEventListener; 38 | onContextmenu?: VugelEventListener; 39 | onDblclick?: VugelEventListener; 40 | onMousedown?: VugelEventListener; 41 | onMouseenter?: VugelEventListener; 42 | onMouseleave?: VugelEventListener; 43 | onMousemove?: VugelEventListener; 44 | onMouseout?: VugelEventListener; 45 | onMouseover?: VugelEventListener; 46 | onMouseup?: VugelEventListener; 47 | 48 | onTouchcancel?: VugelEventListener; 49 | onTouchend?: VugelEventListener; 50 | onTouchmove?: VugelEventListener; 51 | onTouchstart?: VugelEventListener; 52 | 53 | onFocusin?: VugelEventListener; 54 | onFocusout?: VugelEventListener; 55 | onFocus?: VugelEventListener; 56 | onBlur?: VugelEventListener; 57 | 58 | onKeypress?: VugelEventListener; 59 | onKeydown?: VugelEventListener; 60 | onKeyup?: VugelEventListener; 61 | }; 62 | 63 | export class Node extends Base { 64 | public readonly stage: VugelStage; 65 | 66 | public _nodeEvents?: NodeEvents = undefined; 67 | private pointerEvents: boolean | undefined = undefined; 68 | 69 | /** 70 | * CSS cursor type to be displayed when this node is being hovered. 71 | */ 72 | public "cursor-type": string | undefined = undefined; 73 | 74 | constructor(stage: VugelStage, base?: Element) { 75 | super(base || new Element(stage)); 76 | this.stage = stage; 77 | if (this.element) { 78 | this.element.data = this; 79 | } 80 | } 81 | 82 | getParentNode(): Node | undefined { 83 | let current = this.parent; 84 | while (current && !(current as any).el) { 85 | current = current.parent; 86 | } 87 | return current as Node; 88 | } 89 | 90 | get el(): Element { 91 | return this.element!; 92 | } 93 | 94 | get id() { 95 | return this.el.id; 96 | } 97 | 98 | set id(v: string | undefined) { 99 | this.el.id = v; 100 | } 101 | 102 | get "direct-ref"() { 103 | return this.el.ref; 104 | } 105 | 106 | set "direct-ref"(v: string | undefined) { 107 | // The direct ref can be used to reference children in direct containers. 108 | this.el.ref = v; 109 | } 110 | 111 | get "pointer-events"() { 112 | return this.pointerEvents; 113 | } 114 | 115 | set "pointer-events"(v: boolean | undefined) { 116 | this.pointerEvents = v; 117 | } 118 | 119 | capturePointerEvents(): boolean { 120 | return this.pointerEvents || (this.pointerEvents === undefined && this.getParentCapturePointerEvents()); 121 | } 122 | 123 | private getParentCapturePointerEvents() { 124 | const parentNode = this.getParentNode(); 125 | return parentNode ? parentNode.capturePointerEvents() : true; 126 | } 127 | 128 | // Returns the element containing the texture. Texture clipping and tinting will be applied to this element. 129 | get textureElement(): Element { 130 | return this.element!; 131 | } 132 | 133 | focus() { 134 | this.stage.eventHelpers.focusManager.setFocus(this); 135 | } 136 | 137 | dispatchEvent(event: VugelEvent) { 138 | const vueEventType = eventTranslators[event.type as SupportedEvents]; 139 | 140 | const eventHandler = this._nodeEvents?.[vueEventType] as VugelEventListener; 141 | eventHandler?.({ 142 | ...event, 143 | currentTarget: this, 144 | }); 145 | } 146 | 147 | /** 148 | * Dispatches a bubbled event. 149 | * 150 | * @param event the event to be dispatched 151 | * @param ancestorBubble if you want to fake bubble (for example for a mouseleave event) until a certain ancestor, this field should be used. 152 | * Note that this will also update the target of the event. When setting it to "true", it will effectively bubble all the way down, though updating the target the entire tree. 153 | */ 154 | dispatchBubbledEvent( 155 | event: VugelEvent, 156 | ancestorBubble: Base | true | undefined = undefined, 157 | ) { 158 | const vueEventType = eventTranslators[event.type as SupportedEvents]; 159 | 160 | let currentNode: Base | undefined = this; 161 | while (currentNode !== undefined && currentNode !== ancestorBubble) { 162 | const eventHandler = (currentNode as Node)._nodeEvents?.[vueEventType] as VugelEventListener; 163 | const newEvent = { 164 | ...event, 165 | currentTarget: currentNode, 166 | target: ancestorBubble !== undefined ? currentNode : event.target, 167 | }; 168 | 169 | eventHandler?.(newEvent); 170 | 171 | if (ancestorBubble !== undefined && newEvent.cancelBubble) { 172 | return; 173 | } 174 | 175 | currentNode = currentNode.parent; 176 | } 177 | } 178 | 179 | // Converts the world (canvas) coordinates to an offset w.r.t. to this Node 180 | getLocalOffset(worldX: number, worldY: number) { 181 | return this.el.core.convertWorldCoordsToLocal(worldX, worldY); 182 | } 183 | 184 | get x() { 185 | return this.el.x; 186 | } 187 | 188 | set x(v: number) { 189 | this.el.x = ensureFloat(v); 190 | } 191 | 192 | get "func-x"() { 193 | return this.el.funcX; 194 | } 195 | 196 | set "func-x"(v: RelativeFunction | string | undefined) { 197 | this.el.funcX = ensureRelativeFunction(v); 198 | } 199 | 200 | get y() { 201 | return this.el.y; 202 | } 203 | 204 | set y(v: number) { 205 | this.el.y = v; 206 | } 207 | 208 | get "func-y"() { 209 | return this.el.funcY; 210 | } 211 | 212 | set "func-y"(v: RelativeFunction | string | undefined) { 213 | this.el.funcY = ensureRelativeFunction(v); 214 | } 215 | 216 | get w() { 217 | return this.el.w; 218 | } 219 | 220 | set w(v: number) { 221 | this.el.w = v; 222 | } 223 | 224 | get "func-w"() { 225 | return this.el.funcW; 226 | } 227 | 228 | set "func-w"(v: RelativeFunction | string | undefined) { 229 | this.el.funcW = ensureRelativeFunction(v); 230 | } 231 | 232 | // This can be used to apply properties directly to this node. Can be handy when the setting relies on the stage. 233 | set "apply-direct"(v: (e: { node: Node }) => void) { 234 | v({ node: this }); 235 | } 236 | 237 | get h() { 238 | return this.el.h; 239 | } 240 | 241 | set h(v: number) { 242 | this.el.h = v; 243 | } 244 | 245 | get "func-h"() { 246 | return this.el.funcH; 247 | } 248 | 249 | set "func-h"(v: RelativeFunction | string | undefined) { 250 | this.el.funcH = ensureRelativeFunction(v); 251 | } 252 | 253 | get "scale-x"() { 254 | return this.el.scaleX; 255 | } 256 | 257 | set "scale-x"(v: number) { 258 | this.el.scaleX = ensureFloat(v); 259 | } 260 | 261 | get "scale-y"() { 262 | return this.el.scaleY; 263 | } 264 | 265 | set "scale-y"(v: number) { 266 | this.el.scaleY = ensureFloat(v); 267 | } 268 | 269 | get scale() { 270 | return this.el.scale; 271 | } 272 | 273 | set scale(v: any) { 274 | this.el.scale = ensureFloat(v); 275 | } 276 | 277 | get "pivot-x"() { 278 | return this.el.pivotX; 279 | } 280 | 281 | set "pivot-x"(v: number) { 282 | this.el.pivotX = ensureFloat(v); 283 | } 284 | 285 | get "pivot-y"() { 286 | return this.el.pivotY; 287 | } 288 | 289 | set "pivot-y"(v: number) { 290 | this.el.pivotY = ensureFloat(v); 291 | } 292 | 293 | get pivot() { 294 | return this.el.pivot; 295 | } 296 | 297 | set pivot(v: any) { 298 | this.el.pivot = ensureFloat(v); 299 | } 300 | 301 | get "mount-x"() { 302 | return this.el.mountX; 303 | } 304 | 305 | set "mount-x"(v: number) { 306 | this.el.mountX = ensureFloat(v); 307 | } 308 | 309 | get "mount-y"() { 310 | return this.el.mountY; 311 | } 312 | 313 | set "mount-y"(v: number) { 314 | this.el.mountY = ensureFloat(v); 315 | } 316 | 317 | get mount() { 318 | return this.el.mount; 319 | } 320 | 321 | set mount(v: any) { 322 | this.el.mount = ensureFloat(v); 323 | } 324 | 325 | get rotation() { 326 | return this.el.rotation; 327 | } 328 | 329 | set rotation(v: number) { 330 | this.el.rotation = ensureFloat(v); 331 | } 332 | 333 | get alpha() { 334 | return this.el.alpha; 335 | } 336 | 337 | set alpha(v: number) { 338 | this.el.alpha = ensureFloat(v); 339 | } 340 | 341 | get visible() { 342 | return this.el.visible; 343 | } 344 | 345 | set visible(v: boolean) { 346 | this.el.visible = ensureBoolean(v); 347 | } 348 | 349 | get "color-top-left"() { 350 | return this.textureElement.colorUl; 351 | } 352 | 353 | set "color-top-left"(v: number | string) { 354 | this.textureElement.colorUl = ensureColor(v); 355 | } 356 | 357 | get "color-top-right"() { 358 | return this.textureElement.colorUr; 359 | } 360 | 361 | set "color-top-right"(v: number | string) { 362 | this.textureElement.colorUr = ensureColor(v); 363 | } 364 | 365 | get "color-bottom-left"() { 366 | return this.textureElement.colorBl; 367 | } 368 | 369 | set "color-bottom-left"(v: number | string) { 370 | this.textureElement.colorBl = ensureColor(v); 371 | } 372 | 373 | get "color-bottom-right"() { 374 | return this.textureElement.colorBr; 375 | } 376 | 377 | set "color-bottom-right"(v: number | string) { 378 | this.textureElement.colorBr = ensureColor(v); 379 | } 380 | 381 | get "color-top"() { 382 | return this.textureElement.colorTop; 383 | } 384 | 385 | set "color-top"(v: number | string) { 386 | this.textureElement.colorTop = ensureColor(v); 387 | } 388 | 389 | get "color-bottom"() { 390 | return this.textureElement.colorBottom; 391 | } 392 | 393 | set "color-bottom"(v: number | string) { 394 | this.textureElement.colorBottom = ensureColor(v); 395 | } 396 | 397 | get "color-left"() { 398 | return this.textureElement.colorLeft; 399 | } 400 | 401 | set "color-left"(v: number | string) { 402 | this.textureElement.colorLeft = ensureColor(v); 403 | } 404 | 405 | get "color-right"() { 406 | return this.textureElement.colorRight; 407 | } 408 | 409 | set "color-right"(v: number | string) { 410 | this.textureElement.colorRight = ensureColor(v); 411 | } 412 | 413 | get color() { 414 | return this.textureElement.color; 415 | } 416 | 417 | set color(v: number | string) { 418 | this.textureElement.color = ensureColor(v); 419 | } 420 | 421 | get "clip-x"() { 422 | return this.textureElement.texture?.x || 0; 423 | } 424 | 425 | set "clip-x"(v: number) { 426 | if (this.textureElement.texture) { 427 | this.textureElement.texture.x = v; 428 | } 429 | } 430 | 431 | get "clip-y"() { 432 | return this.textureElement.texture?.y || 0; 433 | } 434 | 435 | set "clip-y"(v: number) { 436 | if (this.textureElement.texture) { 437 | this.textureElement.texture.y = v; 438 | } 439 | } 440 | 441 | get "clip-w"() { 442 | return this.textureElement.texture?.w || 0; 443 | } 444 | 445 | set "clip-w"(v: number) { 446 | if (this.textureElement.texture) { 447 | this.textureElement.texture.w = v; 448 | } 449 | } 450 | 451 | get "clip-h"() { 452 | return this.textureElement.texture?.h || 0; 453 | } 454 | 455 | set "clip-h"(v: number) { 456 | if (this.textureElement.texture) { 457 | this.textureElement.texture.h = v; 458 | } 459 | } 460 | 461 | get "pixel-ratio"() { 462 | return this.textureElement.texture?.pixelRatio || 1; 463 | } 464 | 465 | set "pixel-ratio"(v: number) { 466 | if (this.textureElement.texture) { 467 | this.textureElement.texture.pixelRatio = ensureFloat(v); 468 | } 469 | } 470 | 471 | get "z-index"() { 472 | return this.el.zIndex; 473 | } 474 | 475 | set "z-index"(v: number) { 476 | this.el.zIndex = ensureFloat(v); 477 | } 478 | 479 | get "bounds-margin"() { 480 | return this.el.boundsMargin; 481 | } 482 | 483 | set "bounds-margin"(v: number | undefined) { 484 | this.el.boundsMargin = ensureFloat(v); 485 | } 486 | 487 | get "bounds-margin-left"() { 488 | return this.el.boundsMarginLeft; 489 | } 490 | 491 | set "bounds-margin-left"(v: number) { 492 | this.el.boundsMarginLeft = ensureFloat(v); 493 | } 494 | 495 | get "bounds-margin-top"() { 496 | return this.el.boundsMarginTop; 497 | } 498 | 499 | set "bounds-margin-top"(v: number) { 500 | this.el.boundsMarginTop = ensureFloat(v); 501 | } 502 | 503 | get "bounds-margin-right"() { 504 | return this.el.boundsMarginRight; 505 | } 506 | 507 | set "bounds-margin-right"(v: number) { 508 | this.el.boundsMarginRight = ensureFloat(v); 509 | } 510 | 511 | get "bounds-margin-bottom"() { 512 | return this.el.boundsMarginBottom; 513 | } 514 | 515 | set "bounds-margin-bottom"(v: number) { 516 | this.el.boundsMarginBottom = ensureFloat(v); 517 | } 518 | 519 | get "flex-item"() { 520 | return this.el.flexItem; 521 | } 522 | 523 | set "flex-item"(v: boolean) { 524 | this.el.flexItem = ensureBoolean(v); 525 | } 526 | 527 | get "flex-grow"() { 528 | return this.el.flexGrow; 529 | } 530 | 531 | set "flex-grow"(v: number) { 532 | this.el.flexGrow = ensureFloat(v); 533 | } 534 | 535 | get "flex-shrink"() { 536 | return this.el.flexShrink; 537 | } 538 | 539 | set "flex-shrink"(v: number) { 540 | this.el.flexShrink = ensureFloat(v); 541 | } 542 | 543 | get "flex-align-self"() { 544 | return this.el.flexAlignSelf; 545 | } 546 | 547 | set "flex-align-self"(v: "flex-start" | "flex-end" | "center" | "stretch" | undefined) { 548 | this.el.flexAlignSelf = v; 549 | } 550 | 551 | get margin() { 552 | return this.el.margin; 553 | } 554 | 555 | set margin(v: number) { 556 | this.el.margin = ensureFloat(v); 557 | } 558 | 559 | get "margin-left"() { 560 | return this.el.marginLeft; 561 | } 562 | 563 | set "margin-left"(v: number) { 564 | this.el.marginLeft = ensureFloat(v); 565 | } 566 | 567 | get "margin-right"() { 568 | return this.el.marginRight; 569 | } 570 | 571 | set "margin-right"(v: number) { 572 | this.el.marginRight = ensureFloat(v); 573 | } 574 | 575 | get "margin-top"() { 576 | return this.el.marginTop; 577 | } 578 | 579 | set "margin-top"(v: number) { 580 | this.el.marginTop = ensureFloat(v); 581 | } 582 | 583 | get "margin-bottom"() { 584 | return this.el.marginBottom; 585 | } 586 | 587 | set "margin-bottom"(v: number) { 588 | this.el.marginBottom = ensureFloat(v); 589 | } 590 | 591 | get "min-width"() { 592 | return this.el.minWidth; 593 | } 594 | 595 | set "min-width"(v: number) { 596 | this.el.minWidth = ensureFloat(v); 597 | } 598 | 599 | get "min-height"() { 600 | return this.el.minHeight; 601 | } 602 | 603 | set "min-height"(v: number) { 604 | this.el.minHeight = ensureFloat(v); 605 | } 606 | 607 | get "max-width"() { 608 | return this.el.maxWidth; 609 | } 610 | 611 | set "max-width"(v: number) { 612 | this.el.maxWidth = ensureFloat(v); 613 | } 614 | 615 | get "max-height"() { 616 | return this.el.maxHeight; 617 | } 618 | 619 | set "max-height"(v: number) { 620 | this.el.maxHeight = ensureFloat(v); 621 | } 622 | 623 | get "skip-in-layout"() { 624 | return this.el.skipInLayout; 625 | } 626 | 627 | set "skip-in-layout"(v: boolean) { 628 | this.el.skipInLayout = ensureBoolean(v); 629 | } 630 | 631 | public get nodeEvents(): NodeEvents { 632 | if (!this._nodeEvents) { 633 | this._nodeEvents = {}; 634 | } 635 | return this._nodeEvents; 636 | } 637 | 638 | // Setters for NodeEvents 639 | set onSetup(v: VugelNodeEventListener | undefined) { 640 | this.el.onSetup = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 641 | } 642 | 643 | set onAttach(v: VugelNodeEventListener | undefined) { 644 | this.el.onAttach = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 645 | } 646 | 647 | set onDetach(v: VugelNodeEventListener | undefined) { 648 | this.el.onDetach = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 649 | } 650 | 651 | set onEnabled(v: VugelNodeEventListener | undefined) { 652 | this.el.onEnabled = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 653 | } 654 | 655 | set onDisabled(v: VugelNodeEventListener | undefined) { 656 | this.el.onDisabled = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 657 | } 658 | 659 | set onActive(v: VugelNodeEventListener | undefined) { 660 | this.el.onActive = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 661 | } 662 | 663 | set onInactive(v: VugelNodeEventListener | undefined) { 664 | this.el.onInactive = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 665 | } 666 | 667 | set onTextureError(v: VugelTextureErrorEventListener | undefined) { 668 | this.el.onTextureError = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 669 | } 670 | 671 | set onTextureLoaded(v: VugelTextureEventListener | undefined) { 672 | this.el.onTextureLoaded = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 673 | } 674 | 675 | set onTextureUnloaded(v: VugelTextureEventListener | undefined) { 676 | this.el.onTextureUnloaded = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 677 | } 678 | 679 | set onResize(v: VugelResizeEventListener | undefined) { 680 | this.el.onResize = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 681 | } 682 | 683 | set onUpdate(v: VugelNodeEventListener | undefined) { 684 | this.el.onUpdate = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 685 | } 686 | 687 | set onAfterCalcs(v: VugelNodeEventListener | undefined) { 688 | this.el.onAfterCalcs = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 689 | } 690 | 691 | set onAfterUpdate(v: VugelNodeEventListener | undefined) { 692 | this.el.onAfterUpdate = v ? (e) => v(augmentTree2dElementEvent(e)) : undefined; 693 | } 694 | 695 | // MouseEvent 696 | set onAuxclick(e: VugelEventListener | undefined) { 697 | this.nodeEvents.onAuxclick = e; 698 | } 699 | 700 | set onClick(e: VugelEventListener | undefined) { 701 | this.nodeEvents.onClick = e; 702 | } 703 | 704 | set onContextmenu(e: VugelEventListener | undefined) { 705 | this.nodeEvents.onContextmenu = e; 706 | } 707 | 708 | set onDblclick(e: VugelEventListener | undefined) { 709 | this.nodeEvents.onDblclick = e; 710 | } 711 | 712 | set onMousedown(e: VugelEventListener | undefined) { 713 | this.nodeEvents.onMousedown = e; 714 | } 715 | 716 | set onMouseenter(e: VugelEventListener | undefined) { 717 | this.nodeEvents.onMouseenter = e; 718 | } 719 | 720 | set onMouseleave(e: VugelEventListener | undefined) { 721 | this.nodeEvents.onMouseleave = e; 722 | } 723 | 724 | set onMousemove(e: VugelEventListener | undefined) { 725 | this.nodeEvents.onMousemove = e; 726 | } 727 | 728 | set onMouseout(e: VugelEventListener | undefined) { 729 | this.nodeEvents.onMouseout = e; 730 | } 731 | 732 | set onMouseover(e: VugelEventListener | undefined) { 733 | this.nodeEvents.onMouseover = e; 734 | } 735 | 736 | set onMouseup(e: VugelEventListener | undefined) { 737 | this.nodeEvents.onMouseup = e; 738 | } 739 | 740 | // TouchEvent 741 | set onTouchcancel(e: VugelEventListener | undefined) { 742 | this.nodeEvents.onTouchcancel = e; 743 | } 744 | 745 | set onTouchend(e: VugelEventListener | undefined) { 746 | this.nodeEvents.onTouchend = e; 747 | } 748 | 749 | set onTouchmove(e: VugelEventListener | undefined) { 750 | this.nodeEvents.onTouchmove = e; 751 | } 752 | 753 | set onTouchstart(e: VugelEventListener | undefined) { 754 | this.nodeEvents.onTouchstart = e; 755 | } 756 | 757 | // FocusEvent 758 | set onFocusin(e: VugelEventListener | undefined) { 759 | this.nodeEvents.onFocusin = e; 760 | } 761 | 762 | set onFocusout(e: VugelEventListener | undefined) { 763 | this.nodeEvents.onFocusout = e; 764 | } 765 | 766 | set onFocus(e: VugelEventListener | undefined) { 767 | this.nodeEvents.onFocus = e; 768 | } 769 | 770 | set onBlur(e: VugelEventListener | undefined) { 771 | this.nodeEvents.onBlur = e; 772 | } 773 | 774 | // KeyboardEvent 775 | set onKeypress(e: VugelEventListener | undefined) { 776 | this.nodeEvents.onKeypress = e; 777 | } 778 | 779 | set onKeydown(e: VugelEventListener | undefined) { 780 | this.nodeEvents.onKeydown = e; 781 | } 782 | 783 | set onKeyup(e: VugelEventListener | undefined) { 784 | this.nodeEvents.onKeyup = e; 785 | } 786 | 787 | getLayoutX() { 788 | return this.el.layoutX; 789 | } 790 | 791 | getLayoutY() { 792 | return this.el.layoutY; 793 | } 794 | 795 | getLayoutW() { 796 | return this.el.layoutW; 797 | } 798 | 799 | getLayoutH() { 800 | return this.el.layoutH; 801 | } 802 | } 803 | 804 | /** 805 | * Do not proxify Nodes when using template refs. 806 | * See https://github.com/vuejs/vue-next/pull/1060 807 | */ 808 | (Node.prototype as any)["__v_skip"] = true; 809 | 810 | function ensureRelativeFunction(v: RelativeFunction | string | undefined) { 811 | if (isString(v)) { 812 | // Convert to function. 813 | return convertRelativeFunction(v); 814 | } else { 815 | return v; 816 | } 817 | } 818 | 819 | // We hold a cache because string-based vue properties will otherwise result in a new relative function on every vnode 820 | // update, with performance implications. 821 | const cachedRelFunctions = new Map(); 822 | function convertRelativeFunction(body: string): RelativeFunction { 823 | let fn = cachedRelFunctions.get(body); 824 | if (!fn) { 825 | fn = new Function("w", "h", `return ${body}`) as RelativeFunction; 826 | cachedRelFunctions.set(body, fn); 827 | } 828 | return fn; 829 | } 830 | -------------------------------------------------------------------------------- /src/runtime/nodes/Paragraph.ts: -------------------------------------------------------------------------------- 1 | import { Node } from "./Node"; 2 | import { ensureColor, ensureFloat } from "../utils/TypeUtils"; 3 | import { Element, TextTexture } from "tree2d"; 4 | import { TextTextureSettings } from "./textures"; 5 | import { Delegator } from "../utils/Delegator"; 6 | import { VugelStage } from "../../wrapper"; 7 | 8 | class Paragraph extends Node { 9 | private settings = new TextTextureSettings(() => this.update()); 10 | 11 | private _text: string = ""; 12 | private _lineHeight: number = 0; 13 | private _fontColor: number = 0xffffffff; 14 | 15 | constructor(stage: VugelStage) { 16 | super(stage); 17 | this.el.flex = true; 18 | this.el.flexWrap = true; 19 | } 20 | 21 | get "line-height"() { 22 | return this._lineHeight; 23 | } 24 | 25 | set "line-height"(v: number) { 26 | this._lineHeight = ensureFloat(v); 27 | this.update(); 28 | } 29 | 30 | get "font-color"() { 31 | return this._fontColor; 32 | } 33 | 34 | set "font-color"(v: number | string) { 35 | this._fontColor = ensureColor(v); 36 | this.update(); 37 | } 38 | 39 | get text() { 40 | return this._text; 41 | } 42 | 43 | set text(text: string) { 44 | this._text = text; 45 | this.update(); 46 | } 47 | 48 | setElementText(text: string) { 49 | this._text = text.trim(); 50 | this.update(); 51 | } 52 | 53 | static newlinePattern = "@+~^"; 54 | 55 | private update() { 56 | const s = this.settings.textSettings; 57 | const fontSize = s.fontSize || 24; 58 | const lineHeight = Math.round(this._lineHeight || fontSize * 1.3); 59 | const letterSpacing = Math.round(fontSize * 0.2); 60 | const margin = Math.round(lineHeight - fontSize); 61 | const fontColor = this._fontColor || 0xffffffff; 62 | 63 | const text = this._text.replace(/(\r?\n)/, ` ${Paragraph.newlinePattern} `); 64 | 65 | const words = text.split(/\s+/); 66 | if (words.length > 1 || words[0] != "") { 67 | const els = words.map((word: string) => { 68 | const el = new Element(this.stage); 69 | if (word === Paragraph.newlinePattern) { 70 | // Force line break. 71 | el.funcW = (w: number) => w; 72 | el.h = 0; 73 | } else { 74 | const texture = new TextTexture(this.stage); 75 | texture.text = word; 76 | texture.setSettings(s); 77 | el.texture = texture; 78 | el.marginRight = letterSpacing; 79 | el.marginTop = Math.round(margin * 0.8); 80 | el.marginBottom = Math.round(margin * 0.2); 81 | el.color = fontColor; 82 | } 83 | return el; 84 | }); 85 | 86 | this.el.childList.setItems(els); 87 | } 88 | } 89 | } 90 | 91 | Delegator.delegate(Paragraph, TextTextureSettings, "settings"); 92 | interface Paragraph extends TextTextureSettings {} 93 | 94 | export { Paragraph }; 95 | -------------------------------------------------------------------------------- /src/runtime/nodes/Root.ts: -------------------------------------------------------------------------------- 1 | import { Container } from "./Container"; 2 | 3 | export class Root extends Container {} 4 | -------------------------------------------------------------------------------- /src/runtime/nodes/TextNode.ts: -------------------------------------------------------------------------------- 1 | import { Base } from "./Base"; 2 | 3 | export class TextNode extends Base { 4 | public readonly text: string; 5 | 6 | constructor(text: string) { 7 | super(undefined); 8 | this.text = text; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/runtime/nodes/direct.ts: -------------------------------------------------------------------------------- 1 | import { Node } from "./Node"; 2 | import { Base } from "./Base"; 3 | import { Element } from "tree2d"; 4 | import { Constructor } from "../utils/TypeUtils"; 5 | import { mixin } from "../utils/mixin"; 6 | import { DirectContainer } from "./DirectContainer"; 7 | import { Rectangle, TextTexture } from "./textures"; 8 | import { Container } from "./Container"; 9 | import { Paragraph } from "./Paragraph"; 10 | 11 | export class DirectContainerMixin extends Container { 12 | _appendChild(child: Base) { 13 | // Ignore virtual dom operations. 14 | } 15 | 16 | _removeChild(child: Base) { 17 | // Ignore virtual dom operations. 18 | } 19 | 20 | _insertBefore(child: Base, anchor: Base) { 21 | // Ignore virtual dom operations. 22 | } 23 | 24 | add(item: DirectNode) { 25 | item.parent = this; 26 | this.containerElement.childList.add(item.el); 27 | } 28 | 29 | itemInList(item: DirectNode): boolean { 30 | return this.containerElement.childList.itemInList(item.el); 31 | } 32 | 33 | addAt(item: DirectNode, index: number) { 34 | item.parent = this; 35 | return this.containerElement.childList.addAt(item.el, index); 36 | } 37 | 38 | replace(item: DirectNode, prevItem: DirectNode) { 39 | return this.containerElement.childList.replace(item.el, prevItem.el); 40 | } 41 | 42 | setAt(item: DirectNode, index: number) { 43 | item.parent = this; 44 | this.containerElement.childList.setAt(item.el, index); 45 | } 46 | 47 | getAt(index: number): DirectNode | undefined { 48 | const el = this.containerElement.childList.getAt(index); 49 | return el ? el.data : undefined; 50 | } 51 | 52 | getIndex(item: DirectNode): number { 53 | return this.containerElement.childList.getIndex(item.el); 54 | } 55 | 56 | getByRef(ref: string): DirectNode | undefined { 57 | return this.containerElement.childList.getByRef(ref)?.data; 58 | } 59 | 60 | remove(item: DirectNode) { 61 | item.parent = undefined; 62 | this.containerElement.childList.remove(item.el); 63 | } 64 | 65 | removeAt(index: number) { 66 | const item = this.containerElement.childList.removeAt(index); 67 | item.data.parent = undefined; 68 | } 69 | 70 | clear() { 71 | this.containerElement.childList.getItems().forEach((item) => (item.data.parent = undefined)); 72 | this.containerElement.childList.clear(); 73 | } 74 | 75 | getChildren(): Node[] { 76 | return this.containerElement.childList.getItems().map((element: Element) => element.data as DirectNode); 77 | } 78 | 79 | getDirectChildren(): DirectNode[] { 80 | return this.getChildren() as DirectNode[]; 81 | } 82 | 83 | setItems(items: DirectNode[]): DirectNode[][] { 84 | const [removed, added] = this.containerElement.childList.setItems(items.map((item) => item.el)); 85 | 86 | items.forEach((item) => (item.parent = this)); 87 | 88 | return [removed.map((e) => e.data as DirectNode), added.map((e) => e.data as DirectNode)]; 89 | } 90 | } 91 | 92 | export type Direct = T & DirectContainer; 93 | 94 | export type DirectNode = Direct | TextTexture | Paragraph; 95 | 96 | export function getDirectType(nodeType: Constructor): Constructor> { 97 | if (nodeType.prototype instanceof Container) { 98 | if (!(nodeType as any).__direct) { 99 | // Create subclass. 100 | const directType = class extends (nodeType as any) {} as Constructor; 101 | mixin(directType, DirectContainer); 102 | (nodeType as any).__direct = directType; 103 | } 104 | return (nodeType as any).__direct; 105 | } else if ((nodeType as any) === Container) { 106 | return DirectContainer as any; 107 | } else { 108 | return nodeType as any; 109 | } 110 | } 111 | 112 | export type ConvertedDirectNode = T extends Container ? Direct : T; 113 | -------------------------------------------------------------------------------- /src/runtime/nodes/effects/BoxBlur.ts: -------------------------------------------------------------------------------- 1 | import { Container } from "../Container"; 2 | import { WebGLBoxBlurShader } from "tree2d"; 3 | import { VugelStage } from "../../../wrapper"; 4 | 5 | export class BoxBlur extends Container { 6 | private shader = new WebGLBoxBlurShader(this.stage.context); 7 | 8 | constructor(stage: VugelStage) { 9 | super(stage); 10 | this.el.shader = this.shader; 11 | this.el.renderToTexture = true; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/runtime/nodes/effects/Grayscale.ts: -------------------------------------------------------------------------------- 1 | import { Container } from "../Container"; 2 | import { WebGLGrayscaleShader } from "tree2d"; 3 | import { ensureFloat } from "../../utils/TypeUtils"; 4 | import { VugelStage } from "../../../wrapper"; 5 | 6 | export class Grayscale extends Container { 7 | private shader = new WebGLGrayscaleShader(this.stage.context); 8 | 9 | constructor(stage: VugelStage) { 10 | super(stage); 11 | this.el.renderToTexture = true; 12 | this.el.shader = this.shader; 13 | } 14 | 15 | get amount() { 16 | return this.shader.amount; 17 | } 18 | 19 | set amount(v: number) { 20 | this.shader.amount = ensureFloat(v); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/runtime/nodes/effects/Rounded.ts: -------------------------------------------------------------------------------- 1 | import { Container } from "../Container"; 2 | import { WebGLRoundedRectangleShader } from "tree2d"; 3 | import { ensureFloat } from "../../utils/TypeUtils"; 4 | import { VugelStage } from "../../../wrapper"; 5 | 6 | export class Rounded extends Container { 7 | private shader = new WebGLRoundedRectangleShader(this.stage.context); 8 | 9 | constructor(stage: VugelStage) { 10 | super(stage); 11 | this.el.shader = this.shader; 12 | this.el.renderToTexture = true; 13 | } 14 | 15 | get radius() { 16 | return this.shader.radius; 17 | } 18 | 19 | set radius(v: number) { 20 | this.shader.radius = ensureFloat(v); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/runtime/nodes/effects/Shader.ts: -------------------------------------------------------------------------------- 1 | import { Container } from "../Container"; 2 | import { Shader as Tree2dShader } from "tree2d"; 3 | import { CoreContext } from "tree2d"; 4 | 5 | export type ShaderFactory = (context: CoreContext) => Tree2dShader | undefined; 6 | 7 | export class Shader extends Container { 8 | get shader() { 9 | return this.el.shader; 10 | } 11 | 12 | set shader(v: Tree2dShader | undefined) { 13 | this.el.shader = v; 14 | } 15 | 16 | set ["shader-factory"](v: ShaderFactory) { 17 | this.shader = v(this.stage.context); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/runtime/nodes/effects/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./Grayscale"; 2 | export * from "./Rounded"; 3 | export * from "./BoxBlur"; 4 | export * from "./Shader"; 5 | -------------------------------------------------------------------------------- /src/runtime/nodes/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./Node"; 2 | export { Container } from "./Container"; 3 | export { Paragraph } from "./Paragraph"; 4 | export { DirectContainer } from "./DirectContainer"; 5 | 6 | export * from "./textures"; 7 | export * from "./effects"; 8 | export * from "./direct"; 9 | -------------------------------------------------------------------------------- /src/runtime/nodes/textures/Drawing.ts: -------------------------------------------------------------------------------- 1 | import { Element } from "tree2d"; 2 | import { DrawingFunction, DrawingTexture } from "tree2d"; 3 | import { DynamicSizeTexture } from "./DynamicSizeTexture"; 4 | import { VugelStage } from "../../../wrapper"; 5 | 6 | export { DrawingFunction }; 7 | 8 | export class Drawing extends DynamicSizeTexture { 9 | private drawingTexture = new DrawingTexture(this.stage); 10 | 11 | constructor(stage: VugelStage) { 12 | super(stage); 13 | this.textureElement.texture = this.drawingTexture; 14 | } 15 | 16 | set onDraw(f: DrawingFunction) { 17 | this.drawingTexture.drawingFunction = f; 18 | } 19 | 20 | protected handleResize(element: Element, w: number, h: number) { 21 | this.drawingTexture.canvasWidth = w; 22 | this.drawingTexture.canvasHeight = h; 23 | } 24 | 25 | update() { 26 | this.drawingTexture._changed(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/runtime/nodes/textures/DynamicSizeTexture.ts: -------------------------------------------------------------------------------- 1 | import { Element, Texture } from "tree2d"; 2 | import { VugelStage } from "../../../wrapper"; 3 | import { Container } from "../Container"; 4 | 5 | export abstract class DynamicSizeTexture extends Container { 6 | private wrapper = new Element(this.stage); 7 | private background = new Element(this.stage); 8 | 9 | constructor(stage: VugelStage) { 10 | super(stage); 11 | 12 | this.el.childList.add(this.background); 13 | this.el.childList.add(this.wrapper); 14 | 15 | // Make sure we don't take up space if the el is flex. 16 | this.background.skipInLayout = true; 17 | 18 | this.wrapper.skipInLayout = true; 19 | this.wrapper.funcW = (w: number) => w; 20 | this.wrapper.funcH = (w: number, h: number) => h; 21 | this.containerElement = this.wrapper; 22 | 23 | this.wrapper.onResize = ({ element, w, h }) => this.handleResize(element, w, h); 24 | this.background.onTextureLoaded = ({ element, texture }) => this.handleTextureLoaded(element, texture); 25 | this.background.ref = "background"; 26 | } 27 | 28 | get textureElement(): Element { 29 | return this.background; 30 | } 31 | 32 | protected abstract handleResize(element: Element, w: number, h: number): void; 33 | 34 | private handleTextureLoaded(element: Element, texture: Texture) { 35 | const renderInfo = texture.getSource()?.getRenderInfo(); 36 | this.background.x = -(renderInfo?.offsetX || 0); 37 | this.background.y = -(renderInfo?.offsetY || 0); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/runtime/nodes/textures/Picture.ts: -------------------------------------------------------------------------------- 1 | import { Container } from "../Container"; 2 | import { ImageTexture } from "tree2d"; 3 | import { VugelStage } from "../../../wrapper"; 4 | 5 | export class Picture extends Container { 6 | private tex: ImageTexture; 7 | 8 | constructor(stage: VugelStage) { 9 | super(stage); 10 | this.tex = new ImageTexture(stage); 11 | this.el.texture = this.tex; 12 | } 13 | 14 | get src() { 15 | return this.tex.src; 16 | } 17 | 18 | set src(value: string | undefined) { 19 | this.tex.src = value; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/runtime/nodes/textures/Rectangle.ts: -------------------------------------------------------------------------------- 1 | import { Container } from "../Container"; 2 | import { VugelStage } from "../../../wrapper"; 3 | 4 | export class Rectangle extends Container { 5 | constructor(stage: VugelStage) { 6 | super(stage); 7 | this.el.texture = stage.rectangleTexture; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/runtime/nodes/textures/StyledRectangle.ts: -------------------------------------------------------------------------------- 1 | import { ensureBoolean, ensureColor, ensureFloat, ensureInt } from "../../utils/TypeUtils"; 2 | import { Element } from "tree2d"; 3 | import { RoundRectTexture, RoundRectOptions } from "tree2d"; 4 | import { DynamicSizeTexture } from "./DynamicSizeTexture"; 5 | import { VugelStage } from "../../../wrapper"; 6 | 7 | export class StyledRectangle extends DynamicSizeTexture { 8 | private options: Partial = {}; 9 | 10 | private roundRectTexture: RoundRectTexture = new RoundRectTexture(this.stage); 11 | 12 | constructor(stage: VugelStage) { 13 | super(stage); 14 | this.textureElement.texture = this.roundRectTexture; 15 | } 16 | 17 | get radius() { 18 | return this.options["radius"] ? this.options["radius"][0] : 0; 19 | } 20 | 21 | set radius(radius: number) { 22 | const r = ensureFloat(radius); 23 | this.setOption("radius", [r, r, r, r]); 24 | } 25 | 26 | get "stroke-width"() { 27 | return this.options["strokeWidth"] ? this.options["strokeWidth"] : 0; 28 | } 29 | 30 | set "stroke-width"(v: number) { 31 | this.setOption("strokeWidth", ensureInt(v)); 32 | } 33 | 34 | get "stroke-color"() { 35 | return this.options["strokeColor"] ? this.options["strokeColor"] : 0xffffffff; 36 | } 37 | 38 | set "stroke-color"(v: number | string) { 39 | this.setOption("strokeColor", ensureColor(v)); 40 | } 41 | 42 | get fill() { 43 | return this.options["fill"] ? this.options["fill"] : true; 44 | } 45 | 46 | set fill(v: boolean) { 47 | this.setOption("fill", ensureBoolean(v)); 48 | } 49 | 50 | get "fill-color"() { 51 | return this.options["fillColor"] ? this.options["fillColor"] : 0xffffffff; 52 | } 53 | 54 | set "fill-color"(v: number | string) { 55 | this.setOption("fillColor", ensureColor(v)); 56 | } 57 | 58 | get "shadow-blur"() { 59 | return this.options["shadowBlur"] ? this.options["shadowBlur"] : 0; 60 | } 61 | 62 | set "shadow-blur"(v: number) { 63 | this.setOption("shadowBlur", ensureFloat(v)); 64 | } 65 | 66 | get "shadow-color"() { 67 | return this.options["shadowColor"] ? this.options["shadowColor"] : 0xffffffff; 68 | } 69 | 70 | set "shadow-color"(v: number | string) { 71 | this.setOption("shadowColor", ensureColor(v)); 72 | } 73 | 74 | get "shadow-offset-x"() { 75 | return this.options["shadowOffsetX"] ? this.options["shadowOffsetX"] : 0; 76 | } 77 | 78 | set "shadow-offset-x"(v: number) { 79 | this.setOption("shadowOffsetX", ensureFloat(v)); 80 | } 81 | 82 | get "shadow-offset-y"() { 83 | return this.options["shadowOffsetY"] ? this.options["shadowOffsetY"] : 0; 84 | } 85 | 86 | set "shadow-offset-y"(v: number) { 87 | this.setOption("shadowOffsetY", ensureFloat(v)); 88 | } 89 | 90 | private updateDimensions(w: number, h: number) { 91 | this.options.w = w; 92 | this.options.h = h; 93 | this.updateRoundedRectangleTextureOptions(); 94 | } 95 | 96 | private setOption(property: T, value: RoundRectOptions[T]) { 97 | this.options[property] = value; 98 | this.updateRoundedRectangleTextureOptions(); 99 | } 100 | 101 | private updateRoundedRectangleTextureOptions() { 102 | this.roundRectTexture.options = this.options; 103 | } 104 | 105 | protected handleResize(element: Element, w: number, h: number) { 106 | if (this.options) { 107 | this.updateDimensions(w, h); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/runtime/nodes/textures/Svg.ts: -------------------------------------------------------------------------------- 1 | import { Element } from "tree2d"; 2 | import { SvgTexture, SvgOptions } from "tree2d"; 3 | import { VugelStage } from "../../../wrapper"; 4 | import { DynamicSizeTexture } from "./DynamicSizeTexture"; 5 | 6 | export class Svg extends DynamicSizeTexture { 7 | private options: SvgOptions = { w: 0, h: 0, src: "" }; 8 | 9 | private svgTexture = new SvgTexture(this.stage); 10 | 11 | constructor(stage: VugelStage) { 12 | super(stage); 13 | this.textureElement.texture = this.svgTexture; 14 | } 15 | 16 | get src() { 17 | return this.options.src; 18 | } 19 | 20 | set src(v: string) { 21 | this.options.src = v; 22 | this.updateTextureOptions(); 23 | } 24 | 25 | private updateDimensions(w: number, h: number) { 26 | this.options.w = w; 27 | this.options.h = h; 28 | this.updateTextureOptions(); 29 | } 30 | 31 | private updateTextureOptions() { 32 | this.svgTexture.options = this.options; 33 | } 34 | 35 | protected handleResize(element: Element, w: number, h: number) { 36 | if (this.options) { 37 | this.updateDimensions(w, h); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/runtime/nodes/textures/TextTexture.ts: -------------------------------------------------------------------------------- 1 | import { Node } from "../Node"; 2 | import { TextTexture as Tree2dTextTexture } from "tree2d"; 3 | import { TextTextureSettings } from "./TextTextureSettings"; 4 | import { Delegator } from "../../utils/Delegator"; 5 | import { VugelStage } from "../../../wrapper"; 6 | 7 | class TextTexture extends Node { 8 | private texture = new Tree2dTextTexture(this.stage); 9 | 10 | private settings = new TextTextureSettings(() => this.update()); 11 | 12 | constructor(stage: VugelStage) { 13 | super(stage); 14 | this.el.texture = this.texture; 15 | } 16 | 17 | get text() { 18 | return this.texture.text; 19 | } 20 | 21 | set text(text: string) { 22 | this.texture.text = text; 23 | this.update(); 24 | } 25 | 26 | setElementText(text: string) { 27 | this.texture.text = text.trim(); 28 | } 29 | 30 | private update() { 31 | this.texture.setSettings(this.settings.textSettings); 32 | } 33 | } 34 | 35 | Delegator.delegate(TextTexture, TextTextureSettings, "settings"); 36 | interface TextTexture extends TextTextureSettings {} 37 | 38 | export { TextTexture }; 39 | -------------------------------------------------------------------------------- /src/runtime/nodes/textures/TextTextureSettings.ts: -------------------------------------------------------------------------------- 1 | import { ensureFloat } from "../../utils/TypeUtils"; 2 | import { TextSettings } from "tree2d"; 3 | 4 | export class TextTextureSettings { 5 | public readonly textSettings: Partial = {}; 6 | 7 | constructor(private onChange: () => void) {} 8 | 9 | get "font-size"() { 10 | return this.textSettings.fontSize || 40; 11 | } 12 | 13 | set "font-size"(v: number) { 14 | this.textSettings.fontSize = ensureFloat(v); 15 | this.onChange(); 16 | } 17 | 18 | get "font-style"() { 19 | return this.textSettings.fontStyle || "normal"; 20 | } 21 | 22 | set "font-style"(v: string) { 23 | this.textSettings.fontStyle = v; 24 | this.onChange(); 25 | } 26 | 27 | get "font-weight"() { 28 | return this.textSettings.fontWeight || 400; 29 | } 30 | 31 | set "font-weight"(v: number) { 32 | this.textSettings.fontWeight = ensureFloat(v); 33 | this.onChange(); 34 | } 35 | 36 | get "font-face"() { 37 | return this.textSettings.fontFace || []; 38 | } 39 | 40 | set "font-face"(v: string | string[]) { 41 | if (!Array.isArray(v)) { 42 | v = [v]; 43 | } 44 | this.textSettings.fontFace = v; 45 | this.onChange(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/runtime/nodes/textures/Texture.ts: -------------------------------------------------------------------------------- 1 | import { Container } from "../Container"; 2 | import { VugelStage } from "../../../wrapper"; 3 | import { Texture as Tree2dTexture } from "tree2d"; 4 | 5 | export type TextureFactory = (stage: VugelStage) => Tree2dTexture | undefined; 6 | 7 | export class Texture extends Container { 8 | get texture() { 9 | return this.el.texture; 10 | } 11 | 12 | set texture(v: Tree2dTexture | undefined) { 13 | this.el.texture = v; 14 | } 15 | 16 | set ["texture-factory"](v: TextureFactory) { 17 | this.texture = v(this.stage); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/runtime/nodes/textures/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./Drawing"; 2 | export * from "./DynamicSizeTexture"; 3 | export * from "./Picture"; 4 | export * from "./Rectangle"; 5 | export * from "./StyledRectangle"; 6 | export * from "./Svg"; 7 | export * from "./TextTexture"; 8 | export * from "./TextTextureSettings"; 9 | export * from "./Texture"; 10 | -------------------------------------------------------------------------------- /src/runtime/nodes/tree2dEvents.ts: -------------------------------------------------------------------------------- 1 | import { VugelStage } from "../../wrapper"; 2 | import { Node } from "./Node"; 3 | import { 4 | ElementEventCallback, 5 | ElementTextureErrorEventCallback, 6 | ElementTextureEventCallback, 7 | ElementResizeEventCallback, 8 | } from "tree2d"; 9 | import { Element } from "tree2d"; 10 | 11 | export function augmentTree2dElementEvent( 12 | e: T, 13 | ): AugmentedElementEventObject { 14 | const node = e.element.data as Node; 15 | const stage = node.stage; 16 | return { ...e, node, stage }; 17 | } 18 | 19 | type AugmentedElementEventObject = T & { 20 | stage: VugelStage; 21 | node: Node; 22 | }; 23 | 24 | type Tree2dElementEventObjectBase = { element: Element }; 25 | 26 | type Tree2dElementEventCallback = (e: EventObject) => void; 27 | 28 | export type GetVugelNodeEventObject = T extends Tree2dElementEventCallback 29 | ? AugmentedElementEventObject 30 | : never; 31 | 32 | export type VugelNodeEventObject = GetVugelNodeEventObject; 33 | export type VugelTextureErrorEventObject = GetVugelNodeEventObject; 34 | export type VugelTextureEventObject = GetVugelNodeEventObject; 35 | export type VugelResizeEventObject = GetVugelNodeEventObject; 36 | 37 | type GetVugelNodeEventListener = (event: EventObject) => void; 38 | export type VugelNodeEventListener = GetVugelNodeEventListener; 39 | export type VugelTextureErrorEventListener = GetVugelNodeEventListener; 40 | export type VugelTextureEventListener = GetVugelNodeEventListener; 41 | export type VugelResizeEventListener = GetVugelNodeEventListener; 42 | -------------------------------------------------------------------------------- /src/runtime/nodes/types.ts: -------------------------------------------------------------------------------- 1 | import { Picture } from "./textures"; 2 | import { Rectangle } from "./textures"; 3 | import { TextTexture } from "./textures"; 4 | import { Base } from "./Base"; 5 | import { Paragraph } from "./Paragraph"; 6 | import { Container } from "./Container"; 7 | import { StyledRectangle } from "./textures"; 8 | import { Drawing } from "./textures"; 9 | import { Texture } from "./textures"; 10 | import { Svg } from "./textures"; 11 | import { Grayscale } from "./effects"; 12 | import { Rounded } from "./effects"; 13 | import { Shader } from "./effects"; 14 | import { BoxBlur } from "./effects"; 15 | import { VugelStage } from "../../wrapper"; 16 | import { DirectContainer } from "./DirectContainer"; 17 | 18 | export const types: Record Base> = { 19 | container: Container, 20 | 21 | // textures 22 | picture: Picture, 23 | rectangle: Rectangle, 24 | text: TextTexture, 25 | "styled-rectangle": StyledRectangle, 26 | drawing: Drawing, 27 | texture: Texture, 28 | svg: Svg, 29 | 30 | // effects 31 | grayscale: Grayscale, 32 | rounded: Rounded, 33 | "box-blur": BoxBlur, 34 | shader: Shader, 35 | 36 | // advanced 37 | paragraph: Paragraph, 38 | "direct-container": DirectContainer, 39 | } as const; 40 | -------------------------------------------------------------------------------- /src/runtime/patchProp.ts: -------------------------------------------------------------------------------- 1 | import { ComponentInternalInstance, SuspenseBoundary, VNode } from "@vue/runtime-core"; 2 | import { Base } from "./nodes/Base"; 3 | 4 | export function patchProp( 5 | el: Base, 6 | key: string, 7 | prevValue: any, 8 | nextValue: any, 9 | isSVG: boolean, 10 | prevChildren?: VNode[], 11 | parentComponent?: ComponentInternalInstance, 12 | parentSuspense?: SuspenseBoundary, 13 | unmountChildren?: any, 14 | ) { 15 | getSetter(key)(el, nextValue); 16 | } 17 | 18 | type SetterFunction = (el: Base, value: any) => void; 19 | 20 | const obj: Record = {}; 21 | 22 | const getSetter = (key: string) => { 23 | if (!obj[key]) { 24 | obj[key] = new Function("el", "value", `el["${key}"] = value`) as SetterFunction; 25 | } 26 | return obj[key]; 27 | }; 28 | -------------------------------------------------------------------------------- /src/runtime/utils/Delegator.ts: -------------------------------------------------------------------------------- 1 | import { AnyClass } from "./TypeUtils"; 2 | 3 | export class Delegator { 4 | public static delegate(base: T, delegate: U, property: string) { 5 | const descriptors = Object.getOwnPropertyDescriptors(delegate.prototype); 6 | const names = Object.keys(descriptors); 7 | for (let i = 0, n = names.length; i < n; i++) { 8 | const name = names[i]; 9 | if (name !== "constructor") { 10 | this.delegateProperty(name, descriptors[name], base.prototype, property); 11 | } 12 | } 13 | } 14 | 15 | private static delegateProperty(name: string, descriptor: PropertyDescriptor, obj: any, objProperty: string) { 16 | const proxyDescriptor: PropertyDescriptor = { ...descriptor }; 17 | if (proxyDescriptor.get) { 18 | proxyDescriptor.get = function () { 19 | return (this as any)[objProperty][name]; 20 | }; 21 | } 22 | if (proxyDescriptor.set) { 23 | proxyDescriptor.set = function (v: any) { 24 | (this as any)[objProperty][name] = v; 25 | }; 26 | } 27 | if (proxyDescriptor.value && proxyDescriptor.value instanceof Function) { 28 | proxyDescriptor.value = function (...args: any[]) { 29 | return (this as any)[objProperty][name](...args); 30 | }; 31 | } 32 | Object.defineProperty(obj, name, proxyDescriptor); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/runtime/utils/TypeUtils.ts: -------------------------------------------------------------------------------- 1 | import { ColorUtils } from "tree2d"; 2 | 3 | export type Constructor = new (...args: any[]) => T; 4 | export type AnyClass = Constructor; 5 | 6 | export function ensureColor(v: any): number { 7 | return ColorUtils.getArgbFromAny(v); 8 | } 9 | 10 | export function ensureInt(v: any): number { 11 | if (typeof v === "number") { 12 | return v; 13 | } else { 14 | return parseInt(v as any) || 0; 15 | } 16 | } 17 | 18 | export function ensureFloat(v: any): number { 19 | if (typeof v === "number") { 20 | return v; 21 | } else { 22 | return parseFloat(v as any) || 0.0; 23 | } 24 | } 25 | 26 | export function ensureBoolean(v: any): boolean { 27 | return v !== "false" && !!v; 28 | } 29 | 30 | export function isString(value: any): value is string { 31 | return typeof value === "string"; 32 | } 33 | 34 | export function isNumber(value: any): value is number { 35 | return typeof value === "number"; 36 | } 37 | 38 | export function parseFloatStrict(value: string) { 39 | if (/^([-+])?([0-9]+(\.[0-9]+)?|Infinity)$/.test(value)) return Number(value); 40 | return NaN; 41 | } 42 | -------------------------------------------------------------------------------- /src/runtime/utils/index.ts: -------------------------------------------------------------------------------- 1 | export { Delegator } from "./Delegator"; 2 | export { mixin } from "./mixin"; 3 | export * from "./TypeUtils"; 4 | -------------------------------------------------------------------------------- /src/runtime/utils/mixin.ts: -------------------------------------------------------------------------------- 1 | import { AnyClass } from "./TypeUtils"; 2 | 3 | export function mixin(target: AnyClass, source: AnyClass) { 4 | Object.getOwnPropertyNames(source.prototype).forEach((name) => { 5 | Object.defineProperty(target.prototype, name, Object.getOwnPropertyDescriptor(source.prototype, name)!); 6 | }); 7 | } 8 | -------------------------------------------------------------------------------- /src/wrapper.ts: -------------------------------------------------------------------------------- 1 | import { createRendererForStage } from "./runtime"; 2 | import { 3 | defineComponent, 4 | Fragment, 5 | watchEffect, 6 | h, 7 | onMounted, 8 | Ref, 9 | ref, 10 | getCurrentInstance, 11 | RootRenderFunction, 12 | } from "@vue/runtime-core"; 13 | import { Stage } from "tree2d"; 14 | import { EventHelpers, setupEvents } from "./events"; 15 | import { Root } from "./runtime/nodes/Root"; 16 | import { nextTick } from "vue"; 17 | 18 | export type VugelStage = Stage & { eventHelpers: EventHelpers }; 19 | 20 | export const Vugel = defineComponent({ 21 | props: { 22 | settings: { type: Object }, 23 | position: { type: String, default: "relative" }, 24 | }, 25 | setup(props, setupContext) { 26 | const elRef: Ref = ref(); 27 | 28 | const maxWidth = ref(4096); 29 | const maxHeight = ref(4096); 30 | 31 | const vugelComponentInstance = getCurrentInstance()!; 32 | 33 | onMounted(() => { 34 | let vugelRenderer: RootRenderFunction; 35 | let stage: VugelStage; 36 | let stageRoot: Root; 37 | 38 | if (elRef.value) { 39 | stage = new Stage(elRef.value, { ...props.settings }) as VugelStage; 40 | stage.eventHelpers = setupEvents(props.settings?.eventsTarget || elRef.value, stage); 41 | 42 | vugelRenderer = createRendererForStage(stage); 43 | stageRoot = new Root(stage, stage.root); 44 | 45 | // Auto-inherit dimensions. 46 | stageRoot["func-w"] = (w: number) => w; 47 | stageRoot["func-h"] = (w: number, h: number) => h; 48 | 49 | // Keep correct aspect-ratio issues when the page is zoomed out. 50 | const maxTextureSize = stage.getMaxTextureSize(); 51 | maxWidth.value = maxTextureSize / stage.pixelRatio; 52 | maxHeight.value = maxTextureSize / stage.pixelRatio; 53 | } 54 | 55 | const defaultSlot = setupContext.slots.default; 56 | if (defaultSlot) { 57 | // We must wait until nextTick to prevent interference in the effect queue. 58 | nextTick().then(() => { 59 | const node = h(Connector, defaultSlot); 60 | vugelRenderer(node, stageRoot); 61 | }); 62 | } else { 63 | console.warn("No default slot is defined"); 64 | } 65 | }); 66 | 67 | /** 68 | * Since vugel uses its own renderer, the ancestor vue's appContext, root and provides would normally be lost in 69 | * the vugel components. 70 | * 71 | * We can fix this by overriding the component's parent, root, appContext and provides before rendering the slot 72 | * contents. 73 | */ 74 | const Connector = defineComponent({ 75 | setup(props, setupContext) { 76 | const instance = getCurrentInstance()!; 77 | 78 | // @see runtime-core createComponentInstance 79 | instance.parent = vugelComponentInstance; 80 | instance.appContext = vugelComponentInstance.appContext; 81 | instance.root = vugelComponentInstance.root; 82 | (instance as any).provides = (vugelComponentInstance as any).provides; 83 | 84 | const defaultSlot = setupContext.slots.default!; 85 | return () => h(Fragment, defaultSlot()); 86 | }, 87 | }); 88 | 89 | // We need to use a wrapper for flexible size layouting to work with tree2d pixelRatio canvas auto-resizing. 90 | return () => 91 | h( 92 | "div", 93 | { 94 | class: "custom-renderer-wrapper", 95 | style: { position: props.position, maxWidth: maxWidth.value, maxHeight: maxHeight.value }, 96 | }, 97 | [ 98 | h("canvas", { 99 | class: "custom-renderer", 100 | style: { position: "absolute", width: "100%", height: "100%" }, 101 | ref: elRef, 102 | }), 103 | ], 104 | ); 105 | }, 106 | }); 107 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es2015", 4 | "module": "commonjs", 5 | "declaration": true, 6 | "declarationDir": "types", 7 | "sourceMap": true, 8 | "outDir": "./dist", 9 | "strict": true, 10 | "moduleResolution": "node", 11 | "lib": ["esnext", "dom", "es2015.promise"] 12 | }, 13 | "include": ["src"], 14 | "exclude": ["node_modules"] 15 | } 16 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const vueExternals = { 2 | commonjs: "vue", 3 | commonjs2: "vue", 4 | amd: "vue", 5 | root: "Vue", 6 | }; 7 | 8 | module.exports = require("@planning.nl/webpack-config")({ 9 | entry: { 10 | vugel: "./src/index.ts", 11 | }, 12 | externals: { 13 | vue: { ...vueExternals }, 14 | "@vue/runtime-core": { ...vueExternals }, 15 | "@vue/reactivity": { ...vueExternals }, 16 | }, 17 | }); 18 | --------------------------------------------------------------------------------