From 9fee0e0c44a0127bd42ec537fe3b86f8cf5e6fe0 Mon Sep 17 00:00:00 2001 From: Heithem Moumni Date: Sat, 24 Feb 2024 03:15:25 +0100 Subject: [PATCH] add an example project --- MindMap.svelte | 6 +- app.css | 59 ++++++++ example/.gitignore | 24 ++++ example/.vscode/extensions.json | 3 + example/README.md | 47 ++++++ example/index.html | 13 ++ example/jsconfig.json | 32 +++++ example/package.json | 19 +++ example/public/vite.svg | 1 + example/src/App.svelte | 246 ++++++++++++++++++++++++++++++++ example/src/app.css | 0 example/src/assets/svelte.svg | 1 + example/src/main.js | 8 ++ example/src/vite-env.d.ts | 2 + example/svelte.config.js | 7 + example/vite.config.js | 7 + package.json | 3 +- parser/index.js | 213 +++++++++++++++++++++++++++ 18 files changed, 685 insertions(+), 6 deletions(-) create mode 100644 app.css create mode 100644 example/.gitignore create mode 100644 example/.vscode/extensions.json create mode 100644 example/README.md create mode 100644 example/index.html create mode 100644 example/jsconfig.json create mode 100644 example/package.json create mode 100644 example/public/vite.svg create mode 100644 example/src/App.svelte create mode 100644 example/src/app.css create mode 100644 example/src/assets/svelte.svg create mode 100644 example/src/main.js create mode 100644 example/src/vite-env.d.ts create mode 100644 example/svelte.config.js create mode 100644 example/vite.config.js create mode 100644 parser/index.js diff --git a/MindMap.svelte b/MindMap.svelte index ed6e58b..94769b9 100644 --- a/MindMap.svelte +++ b/MindMap.svelte @@ -16,7 +16,7 @@ d3PanZoom, onTick } from "./utils/d3"; - + import './app.css' import { getDimensions, getViewBox } from "./utils/dimensions"; import subnodesToHTML from "./utils/subnodesToHTML"; import nodeToHTML from "./utils/nodeToHTML"; @@ -139,10 +139,6 @@ }); - -
{#if map.title}

{map.title}

diff --git a/app.css b/app.css new file mode 100644 index 0000000..1cb09f9 --- /dev/null +++ b/app.css @@ -0,0 +1,59 @@ +.mindmap-svg { + height: 100vh; + width: 100%; +} + .mindmap-svg:focus { + outline: none; +} + .mindmap-node > a { + background: #f5f5f5; + border-radius: 10px; + box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12); + color: #212121; + display: inline-block; + font-family: 'Raleway'; + font-size: 22px; + margin: 0 auto; + padding: 15px; + text-align: center; + text-decoration: none; + transition: background-color 0.2s, color 0.2s ease-out; +} + .mindmap-node > a[href]:hover { + background-color: #f57c00; + color: #fff; + cursor: pointer; +} + .mindmap-node--editable { + cursor: all-scroll; +} + .mindmap-node--editable > a { + pointer-events: none; +} + .mindmap-subnode-group { + align-items: center; + border-left: 4px solid #9e9e9e; + display: flex; + margin-left: 15px; + padding: 5px; +} + .mindmap-subnode-group a { + color: #212121; + font-family: 'Raleway'; + font-size: 16px; + padding: 2px 5px; +} + .mindmap-connection { + fill: transparent; + stroke: #9e9e9e; + stroke-dasharray: 10px 4px; + stroke-width: 3px; +} + .mindmap-emoji { + height: 24px; + vertical-align: bottom; + width: 24px; +} + .reddit-emoji { + border-radius: 50%; +} \ No newline at end of file diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..a547bf3 --- /dev/null +++ b/example/.gitignore @@ -0,0 +1,24 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/example/.vscode/extensions.json b/example/.vscode/extensions.json new file mode 100644 index 0000000..bdef820 --- /dev/null +++ b/example/.vscode/extensions.json @@ -0,0 +1,3 @@ +{ + "recommendations": ["svelte.svelte-vscode"] +} diff --git a/example/README.md b/example/README.md new file mode 100644 index 0000000..382941e --- /dev/null +++ b/example/README.md @@ -0,0 +1,47 @@ +# Svelte + Vite + +This template should help get you started developing with Svelte in Vite. + +## Recommended IDE Setup + +[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode). + +## Need an official Svelte framework? + +Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more. + +## Technical considerations + +**Why use this over SvelteKit?** + +- It brings its own routing solution which might not be preferable for some users. +- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app. + +This template contains as little as possible to get started with Vite + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-vite` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project. + +Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate. + +**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?** + +Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information. + +**Why include `.vscode/extensions.json`?** + +Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project. + +**Why enable `checkJs` in the JS template?** + +It is likely that most cases of changing variable types in runtime are likely to be accidental, rather than deliberate. This provides advanced typechecking out of the box. Should you like to take advantage of the dynamically-typed nature of JavaScript, it is trivial to change the configuration. + +**Why is HMR not preserving my local component state?** + +HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/sveltejs/svelte-hmr/tree/master/packages/svelte-hmr#preservation-of-local-state). + +If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR. + +```js +// store.js +// An extremely simple external store +import { writable } from 'svelte/store' +export default writable(0) +``` diff --git a/example/index.html b/example/index.html new file mode 100644 index 0000000..feaf27c --- /dev/null +++ b/example/index.html @@ -0,0 +1,13 @@ + + + + + + + Vite + Svelte + + +
+ + + diff --git a/example/jsconfig.json b/example/jsconfig.json new file mode 100644 index 0000000..5696a2d --- /dev/null +++ b/example/jsconfig.json @@ -0,0 +1,32 @@ +{ + "compilerOptions": { + "moduleResolution": "bundler", + "target": "ESNext", + "module": "ESNext", + /** + * svelte-preprocess cannot figure out whether you have + * a value or a type, so tell TypeScript to enforce using + * `import type` instead of `import` for Types. + */ + "verbatimModuleSyntax": true, + "isolatedModules": true, + "resolveJsonModule": true, + /** + * To have warnings / errors of the Svelte compiler at the + * correct position, enable source maps by default. + */ + "sourceMap": true, + "esModuleInterop": true, + "skipLibCheck": true, + /** + * Typecheck JS in `.svelte` and `.js` files by default. + * Disable this if you'd like to use dynamic types. + */ + "checkJs": true + }, + /** + * Use global.d.ts instead of compilerOptions.types + * to avoid limiting type declarations. + */ + "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"] +} diff --git a/example/package.json b/example/package.json new file mode 100644 index 0000000..23c58ad --- /dev/null +++ b/example/package.json @@ -0,0 +1,19 @@ +{ + "name": "vite-svelte-starter", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": "vite build", + "preview": "vite preview" + }, + "devDependencies": { + "@sveltejs/vite-plugin-svelte": "^3.0.2", + "svelte": "^4.2.11", + "vite": "^5.1.4" + }, + "dependencies": { + "svelte-mindmap": "^1.0.4" + } +} diff --git a/example/public/vite.svg b/example/public/vite.svg new file mode 100644 index 0000000..e7b8dfb --- /dev/null +++ b/example/public/vite.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/example/src/App.svelte b/example/src/App.svelte new file mode 100644 index 0000000..bb858e4 --- /dev/null +++ b/example/src/App.svelte @@ -0,0 +1,246 @@ + + +
+
+ +
+
+ + diff --git a/example/src/app.css b/example/src/app.css new file mode 100644 index 0000000..e69de29 diff --git a/example/src/assets/svelte.svg b/example/src/assets/svelte.svg new file mode 100644 index 0000000..c5e0848 --- /dev/null +++ b/example/src/assets/svelte.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/example/src/main.js b/example/src/main.js new file mode 100644 index 0000000..8a909a1 --- /dev/null +++ b/example/src/main.js @@ -0,0 +1,8 @@ +import './app.css' +import App from './App.svelte' + +const app = new App({ + target: document.getElementById('app'), +}) + +export default app diff --git a/example/src/vite-env.d.ts b/example/src/vite-env.d.ts new file mode 100644 index 0000000..4078e74 --- /dev/null +++ b/example/src/vite-env.d.ts @@ -0,0 +1,2 @@ +/// +/// diff --git a/example/svelte.config.js b/example/svelte.config.js new file mode 100644 index 0000000..b0683fd --- /dev/null +++ b/example/svelte.config.js @@ -0,0 +1,7 @@ +import { vitePreprocess } from '@sveltejs/vite-plugin-svelte' + +export default { + // Consult https://svelte.dev/docs#compile-time-svelte-preprocess + // for more information about preprocessors + preprocess: vitePreprocess(), +} diff --git a/example/vite.config.js b/example/vite.config.js new file mode 100644 index 0000000..d701969 --- /dev/null +++ b/example/vite.config.js @@ -0,0 +1,7 @@ +import { defineConfig } from 'vite' +import { svelte } from '@sveltejs/vite-plugin-svelte' + +// https://vitejs.dev/config/ +export default defineConfig({ + plugins: [svelte()], +}) diff --git a/package.json b/package.json index cc7970e..6bb3aa5 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "SvelteMindmap is a svelte component for mindnode maps", "main": "MindMap.svelte", "dependencies": { - "d3": "^6.3.1" + "d3": "^7.8.5", + "fs-walk": "^0.0.2" }, "repository": { "type": "git", diff --git a/parser/index.js b/parser/index.js new file mode 100644 index 0000000..8a7a6a8 --- /dev/null +++ b/parser/index.js @@ -0,0 +1,213 @@ +#!/usr/bin/env node +const path = require('path'); +const fs = require('fs'); +const walkSync = require('fs-walk').walkSync; + +const { emojiToCategory, matchEmojis } = require('./emojis'); +const { getText, getURL } = require('./regex'); + +// These two arguments must be directories. +const input = process.argv[2]; +const output = process.argv[3]; + +// Using for converting URLs with IDs to full URLs. +const mapsLookup = {}; + +if (input === undefined || output === undefined) { + // eslint-disable-next-line no-console + console.log( + 'No files were parsed due to insufficient arguments \nPlease run the parser with the following command: npm run parse "path/to/mindmap/json/folder" "path/to/output/folder"' + ); + process.exit(); +} + +/* + * Equivalent to mkdir -p dirname. + */ +const mkdirs = (dirname) => { + const parentDir = path.dirname(dirname); + + if (!fs.existsSync(parentDir)) { + mkdirs(parentDir); + } + + fs.mkdirSync(dirname); +}; + +/* + * Recursively walk a directory and call a function on all its files. + * Imported file and absolute path are the parameters passed to the callback function. + */ +const walkDir = (dirname, fn) => { + walkSync(dirname, (basedir, filename, stat) => { + const absPath = path.resolve('./', basedir, filename); + + if (stat.isDirectory()) { + return walkDir(absPath, fn); + } + + if (typeof fn === 'function') { + // eslint-disable-next-line import/no-dynamic-require, global-require + fn(require(absPath), absPath); + } + + return null; + }); +}; + +/* + * Take a node from MindNode format and return it in the following format: + * + * { + * text: string, + * url: string, + * note: string || undefined, + * fx: number, + * fy: number, + * category: string, + * } + */ +const parseNode = (node) => { + const parsedNode = { + text: getText(node.title.text), + note: node.note ? getText(node.note.text) : undefined, + url: getURL(node.title.text), + fx: node.location.x, + fy: node.location.y, + // eslint-disable-next-line no-use-before-define + nodes: parseSubnodes(node.nodes), + }; + + // If URL is an internal URL that uses the map ID, switch it to + // the full URL with the path. + const matchInternalURL = parsedNode.url.match(/\/id\/(\S{40})/); + if (matchInternalURL) { + parsedNode.url = mapsLookup[matchInternalURL[1]]; + } + + if (parsedNode.note) { + parsedNode.note = parsedNode.note.replace( + 'if you think this can be improved in any way please say', + '' + ); + } + + const match = parsedNode.text.match(matchEmojis); + + if (match) { + parsedNode.category = emojiToCategory(match[0]); + parsedNode.text = parsedNode.text.replace(matchEmojis, '').trim(); + } + + return parsedNode; +}; + +/* + * Parse an array of subnodes recursively. Uses parseNode, so the structure of + * subnodes will be the same. The only difference is a color attribute on + * subnodes, which has a string with a valid CSS color format. + */ +const parseSubnodes = (subnodes) => + subnodes.map((subnode) => { + const parsedSub = parseNode(subnode); + + if (subnode.shapeStyle && subnode.shapeStyle.borderStrokeStyle) { + parsedSub.color = subnode.shapeStyle.borderStrokeStyle.color; + } + + parsedSub.nodes = parseSubnodes(subnode.nodes); + return parsedSub; + }); + +/* + * Take a connection from MindNode format and return it in the following format: + * + * { + * text: string, + * source: string, + * target: string, + * curve: { + * x: number, + * y: number, + * }, + * } + * + * source and target are the text attributes of the nodes the connection links. + * curve is the location of the focal point for a bezier curve. + */ +const parseConn = (conn, lookup) => { + const parsedConn = { + source: lookup[conn.startNodeID], + target: lookup[conn.endNodeID], + curve: { + x: conn.wayPointOffset.x, + y: conn.wayPointOffset.y, + }, + }; + + if (conn.title && conn.title.text) { + parsedConn.text = getText(conn.title.text); + } + + return parsedConn; +}; + +walkDir(input, (map, filename) => { + const inputBasePath = `${path.resolve('./', input)}`; + let relativeFilePath = filename + .replace(inputBasePath, '') + .replace('.json', ''); + + if (relativeFilePath !== '/learn-anything') { + relativeFilePath = relativeFilePath.replace( + '/learn-anything', + '' + ); + } + + mapsLookup[map.token] = relativeFilePath; +}); + +walkDir(input, (map, filename) => { + const nodesLookup = {}; + const splitTitle = map.title.split('-'); + let trigger = map.trigger; + + if (splitTitle[splitTitle.length - 1].trim(' ') === trigger) { + trigger = ''; + } + + const parsedMap = { + title: map.title, + tag: trigger, + description: map.description, + }; + + // Parse all nodes and populate the lookup table, which will be used for + // converting IDs to node title on connections. + parsedMap.nodes = map.nodes.map((node) => { + const parsedNode = parseNode(node); + nodesLookup[node.id] = parsedNode.text; + return parsedNode; + }); + + parsedMap.connections = map.connections.map((conn) => + parseConn(conn, nodesLookup) + ); + + // Find out the path for the output file. + const inputBasePath = path.resolve('./', input); + const outputFile = path.join( + output, + filename.replace(inputBasePath, '') + ); + const outputPath = path.dirname(outputFile); + + // Create folder if it doesn't exist. + if (!fs.existsSync(outputPath)) { + mkdirs(outputPath); + } + + // Write parsed map to new location. + fs.writeFileSync(outputFile, JSON.stringify(parsedMap, null, 2)); +});