iCode Documentation
website·Indexed 30 days ago
https://lxuan1997.github.io/iCode/A collection of technical notes and guides covering frontend development and tooling, including Vue.js, Pinia, Vue Router, JavaScript, CSS, Git, GitHub Actions, Nginx, ECharts, and Fabric.js integration.
What's inside iCode
- Pinia is the officially recommended state management library for Vue, supporting both Vue 2 and Vue 3. It serves as the successor to Vuex, offering a simpler API, support for the Composition API style, and robust TypeScript type inference.
Implement selective page caching using keep-alive and route meta properties
To cache specific pages in a Vue application, use the
<keep-alive>component in conjunction with themetaproperty in the Vue Router configuration. By checking for a custom property (e.g.,keepAlive) in$route.meta, you can determine whether a component should be cached or re-rendered.Critical Warning: Do not place the
v-ifdirective directly on the<keep-alive>component; instead, place it on the<router-view>inside the<keep-alive>wrapper to ensure the caching mechanism functions correctly.<!-- App.vue or router configuration template --> <template> <div > <keep-alive> <router-view v-if="$route.meta.keepAlive"></router-view> </keep-alive> <router-view v-if="!$route.meta.keepAlive"></router-view> </div> </template> <script> // router.js configuration const routes = [ { path: '/cache', component: CacheComponent, meta: { keepAlive: true } }, { path: '/no-cache', component: NoCacheComponent, meta: { keepAlive: false } } ] </script>Perform a Git cherry-pick in IntelliJ IDEA
Use thecherry-pickfeature in IntelliJ IDEA when you need to select and move specific commits from one Git branch to another without merging the entire branch. This is particularly useful for collaborative environments where only a subset of changes from a feature branch is required in the target branch.Implement Tree-Shaking in JavaScript modules
Tree-Shaking is the process of removing dead code (code that will never be executed). For Tree-Shaking to work, modules must use ESM (ES Modules) because the process relies on the static structure of ESM.
Because JavaScript is dynamic, static analysis cannot always determine if a function call has side effects (e.g., modifying a global variable). To explicitly tell build tools like Rollup.js or Webpack that a piece of code is pure and has no side effects, add the
/*#__PURE__*/comment before the function call./*#__PURE__*/ someFunction();Remove the background color from autofilled input fields
Use the-webkit-autofillpseudo-class and set-webkit-background-clip: textto remove the default browser background color when an input is autofilled.input:-webkit-autofill, input:-webkit-autofill:hover, input:-webkit-autofill:active, input:-webkit-autofill:focus { -webkit-background-clip: text; }Automate daily image scraping from Bing using GitHub Actions
This implementation uses a scheduled GitHub Action to fetch the daily image from Bing and prepend it to aREADME.mdfile. The workflow is configured to run daily at 00:30 UTC, install dependencies, execute a Node.js script, and commit the changes back to the main branch.name: Node.js CI on: push: branches: [ main ] pull_request: branches: [ main ] schedule: - cron: '30 0 * * *' jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: [ 14.x ] steps: - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm run start --if-present - name: Commit git run: | git config --local user.email 'lxuan2497@gmail.com' git config --local user.name 'lxuan' git pull git add README.md git commit -m 'update README.md' - name: Push changes uses: ad-m/github-push-action@master with: github_token: ${{ secrets.GITHUB_TOKEN }} branch: mainCompare Pinia Option and Composition syntax
Pinia supports two different syntaxes for defining stores. The Option API style mirrors the Vue component data/computed/methods structure, while the Composition API (Setup) style uses standard Vue reactivity primitives.Store Concept Option API (data) Composition API (setup) state data ref() getters computed computed() action methods function() Add images to a Fabric canvas
Usefabric.Image.fromURLto load an image. It is common to calculate an initial scale based on the image width to ensure it fits the desired canvas area.function addImage(url) { fabric.Image.fromURL(url, (image) => { const {width} = image const initialScale = 200 / width image.set({ scaleX: initialScale, scaleY: initialScale, // options }) canvas.add(image) canvas.setActiveObject(image) }) }Search for code within file contents on GitHub
To search for specific code or text within the contents of files in a GitHub repository, use thein:filequalifier in the GitHub search bar.in:fileUpload a local repository to a remote Git server
Follow these steps to initialize a local directory as a Git repository, commit the files, and push them to a remote server (such as GitHub or Gitee).# Initialize local repository git init # Add all modified files to the staging area git add . # Commit changes with a descriptive message git commit -m '项目初始化' # Connect to the remote repository git remote add <shortname> <url> # Push the master branch to the remote server git push -u <shortname> masterImport and export Fabric canvas data
Canvas state can be exported to a JSON string usingtoJSON()and restored usingloadFromJSON().// Import data function dataImport(data) { canvas.loadFromJSON(data, () => { // callback }) } // Export data function dataExport() { return canvas.toJSON() }Save Fabric canvas as an image file
Convert the current canvas state to a Data URL (e.g., JPEG) and trigger a browser download using a temporary anchor element.function saveAsImage() { const dataUrl = canvas.toDataURL('image/jpeg') const link = document.createElement('a') link.download = 'canvas.png' link.href = dataUrl link.click() }