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.

Tokens
10K
Snippets
57
Records
75
Agent score
75%

What's inside iCode

  1. Introduction to Pinia state management

    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.
  2. 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 the meta property 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-if directive 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>
  3. Perform a Git cherry-pick in IntelliJ IDEA

    Use the cherry-pick feature 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.
  4. 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();
  5. Remove the background color from autofilled input fields

    Use the -webkit-autofill pseudo-class and set -webkit-background-clip: text to 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;
    }
  6. 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 a README.md file. 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: main
  7. Compare 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 ConceptOption API (data)Composition API (setup)
    statedataref()
    getterscomputedcomputed()
    actionmethodsfunction()
  8. Add images to a Fabric canvas

    Use fabric.Image.fromURL to 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)
        })
    }
  9. 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 the in:file qualifier in the GitHub search bar.
    in:file
  10. Upload 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> master
  11. Import and export Fabric canvas data

    Canvas state can be exported to a JSON string using toJSON() and restored using loadFromJSON().
    // Import data
    function dataImport(data) {
        canvas.loadFromJSON(data, () => {
            // callback
        })
    }
    
    // Export data
    function dataExport() {
        return canvas.toJSON()
    }
  12. 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()
    }