Sketch Document Utilities

repository·main·Indexed 42 days ago

https://github.com/sketch-hq/sketch-document

A collection of tools for interacting with .sketch files, including @sketch-hq/sketch-file for reading and writing documents, @sketch-hq/sketch-file-format for JSON Schema definitions, and @sketch-hq/sketch-file-format-ts for automatically generated TypeScript type definitions.

Tokens
3.9K
Snippets
17
Records
23
Agent score
52%

What's inside Sketch Document

  1. Understand the schema build process and strictness

    main

    The build process transforms hand-editable YAML source files into valid JSON Schema output. During this process, several transformations are applied to increase schema strictness:

    • Abstract schemas: Used in YAML to maintain DRYness; these are processed out during the build.
    • Additional properties: While JSON Schema defaults additionalProperties to true, the build process sets it to false on every object schema unless explicitly specified otherwise, to increase strictness.
    • Required properties: The build process automatically sets every object property as required. To define a property as genuinely optional, use the non-standard optional keyword in the YAML source; this keyword is removed during the build.
  2. Workflow for updating schemas

    main

    To update the schemas, follow these steps:

    1. Update the schema YAML source files.
    2. Run yarn validate-schemas to ensure changes are valid JSON Schema Draft 7.
    3. Run yarn validate-file and yarn validate-reference-files to test against real Sketch files.
    4. Determine the appropriate semver bump type and run yarn changeset to create a release intent.
    5. Open a PR to main.
  3. Generate TypeScript types for Sketch document elements

    main

    The TypeScript types in this package are generated from the JSON Schema definitions in @sketch-hq/sketch-file-format. To ensure you are using the latest schemas, you should follow one of these two workflows:

    1. Full Monorepo Build: Run yarn build from the monorepo root. This builds @sketch-hq/sketch-file-format first and then generates the TypeScript types.
    2. Type-only Generation: Run yarn generate from the file-format-ts directory. This generates only the TypeScript types based on the existing build of the schemas.
    # From monorepo root to build everything including types
    yarn build
    
    # From packages/file-format-ts to generate only types
    yarn generate
  4. Create a typed layer blur object

    main

    You can use the FileFormat namespace to create strictly typed objects, such as a Blur object, ensuring all required properties like _class, isEnabled, and type are present.

    import FileFormat from '@sketch-hq/sketch-file-format-ts'
    
    const blur: FileFormat.Blur = {
      _class: 'blur',
      isEnabled: false,
      center: '{0.5, 0.5}',
      motionAngle: 0,
      radius: 10,
      saturation: 1,
      type: FileFormat.BlurType.Gaussian,
    }
  5. Use Sketch schemas in JavaScript/TypeScript

    main

    After installing the package, you can import the schemas object to access the JSON Schema definitions. This object contains the schemas for different parts of a Sketch file (like document, page, or meta) and version information.

    import schemas from '@sketch-hq/sketch-file-format'
    
    // schemas contains properties like:
    // schemas.version: Latest supported Sketch document version
    // schemas.document: JSONSchema7 for the document JSON entry
    // schemas.page: JSONSchema7 for page JSON entries
  6. Narrow layer types using AnyLayer

    main

    The package provides helper union types like AnyLayer. You can use the _class property as a discriminant within a switch statement to narrow the type of a layer to its specific implementation (e.g., Bitmap or Star).

    import FileFormat from '@sketch-hq/sketch-file-format-ts'
    
    const mapLayers = (layers: FileFormat.AnyLayer[]) => {
      return layers.map((layer) => {
        switch (layer._class) {
          case 'bitmap':
          // type narrowed to Bitmap layers
          case 'star':
          // type narrowed to Star layers
        }
      })
    }
  7. Write Sketch documents with `toFile`

    main

    Use the toFile function to save changes back to a .sketch file. To use this, you must provide a SketchFile object which contains both the modified contents and the original filepath.

    import { fromFile, SketchFile, toFile } from '@sketch-hq/sketch-file'
    import { resolve } from 'path'
    
    const sketchDocumentPath = './artboards-with-background.sketch'
    
    fromFile(resolve(__dirname, sketchDocumentPath)).then((parsedFile) => {
      // ... perform modifications on parsedFile.contents ...
    
      const exportableFile: SketchFile = {
        contents: parsedFile.contents,
        filepath: sketchDocumentPath,
      }
      
      toFile(exportableFile)
    })