Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

fix: patch CanvasRenderingContext2D for unsupported properties to avoid errors

Ramesh Mane4e10e3705f8c
diff --git a/packages/nc-gui/components/smartsheet/grid/canvas/composables/useCanvasRender.ts b/packages/nc-gui/components/smartsheet/grid/canvas/composables/useCanvasRender.ts
index 3bea65aba7..cb7485608a 100644
--- a/packages/nc-gui/components/smartsheet/grid/canvas/composables/useCanvasRender.ts
+++ b/packages/nc-gui/components/smartsheet/grid/canvas/composables/useCanvasRender.ts
@@ -38,6 +38,7 @@ import { parseKey } from '../../../../../utils/groupbyUtils'
import type { CanvasElement } from '../utils/CanvasElement'
import { ElementTypes } from '../utils/CanvasElement'
import type { RenderTagProps } from '../utils/types'
+import { getSafe2DContext } from '../utils/safeCanvas'
export function useCanvasRender({
width,
@@ -3444,7 +3445,7 @@ export function useCanvasRender({
const canvas = canvasRef.value
if (!canvas) return
- const ctx = canvas.getContext('2d')
+ const ctx = getSafe2DContext(canvas)
if (!ctx) return
const dpr = window.devicePixelRatio || 1
diff --git a/packages/nc-gui/components/smartsheet/grid/canvas/loaders/SpriteLoader.ts b/packages/nc-gui/components/smartsheet/grid/canvas/loaders/SpriteLoader.ts
index f70c59a6da..44ebe5f3a4 100644
--- a/packages/nc-gui/components/smartsheet/grid/canvas/loaders/SpriteLoader.ts
+++ b/packages/nc-gui/components/smartsheet/grid/canvas/loaders/SpriteLoader.ts
@@ -1,6 +1,7 @@
import type { VNode } from 'vue'
import { createSSRApp } from 'vue'
import { renderToString } from 'vue/server-renderer'
+import { getSafe2DContext } from '../utils/safeCanvas'
export class SpriteLoader {
private svgCache = new Map<string, string>()
@@ -59,7 +60,7 @@ export class SpriteLoader {
const canvas = document.createElement('canvas')
canvas.width = size * scale
canvas.height = size * scale
- const spriteCtx = canvas.getContext('2d')!
+ const spriteCtx = getSafe2DContext(canvas)
spriteCtx.imageSmoothingEnabled = true
spriteCtx.imageSmoothingQuality = 'high'
diff --git a/packages/nc-gui/components/smartsheet/grid/canvas/utils/canvas.ts b/packages/nc-gui/components/smartsheet/grid/canvas/utils/canvas.ts
index df42bbdf19..fd9f4159db 100644
--- a/packages/nc-gui/components/smartsheet/grid/canvas/utils/canvas.ts
+++ b/packages/nc-gui/components/smartsheet/grid/canvas/utils/canvas.ts
@@ -5,6 +5,7 @@ import type { SpriteLoader } from '../loaders/SpriteLoader'
import type { RenderMultiLineTextProps, RenderSingleLineTextProps, RenderTagProps } from './types'
import { type Block, getFontForToken, parseMarkdown } from './markdownUtils'
import { NcMarkdownParser } from '~/helpers/tiptap'
+import { getSafe2DContext } from './safeCanvas'
const singleLineTextCache: LRUCache<string, { text: string; width: number; isTruncated: boolean }> = new LRUCache({
max: 1000,
@@ -1666,4 +1667,4 @@ export function renderFormulaURL(
}
const offscreenCanvas = new OffscreenCanvas(0, 0)
-export const defaultOffscreen2DContext = offscreenCanvas.getContext('2d')!
+export const defaultOffscreen2DContext = getSafe2DContext(offscreenCanvas)
diff --git a/packages/nc-gui/components/smartsheet/grid/canvas/utils/safeCanvas.ts b/packages/nc-gui/components/smartsheet/grid/canvas/utils/safeCanvas.ts
new file mode 100644
index 0000000000..aa739f53a5
--- /dev/null
+++ b/packages/nc-gui/components/smartsheet/grid/canvas/utils/safeCanvas.ts
@@ -0,0 +1,107 @@
+export function getSafe2DContext<T extends HTMLCanvasElement | OffscreenCanvas>(
+ canvas: T,
+ throwError = false,
+): T extends HTMLCanvasElement ? CanvasRenderingContext2D : OffscreenCanvasRenderingContext2D {
+ // get 2D context (may be null)
+ const ctx = canvas.getContext('2d') as CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D | null
+
+ if (!ctx) {
+ if (throwError) {
+ throw new Error('2D canvas context not available')
+ }
+
+ return null as any
+ }
+
+ // patch missing functions safely
+ return patchContext(ctx) as any
+}
+
+export function patchContext<T extends CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D>(ctx: T): T {
+ // -----------------------------
+ // roundRect (older browsers)
+ // -----------------------------
+ if (typeof (ctx as any).roundRect !== 'function') {
+ ;(ctx as any).roundRect = function (
+ x: number,
+ y: number,
+ width: number,
+ height: number,
+ radius: number | number[] | { topLeft?: number; topRight?: number; bottomRight?: number; bottomLeft?: number } = 0,
+ ): CanvasRenderingContext2D {
+ let r = {
+ topLeft: 0,
+ topRight: 0,
+ bottomRight: 0,
+ bottomLeft: 0,
+ }
+
+ if (typeof radius === 'number') {
+ r = { topLeft: radius, topRight: radius, bottomRight: radius, bottomLeft: radius }
+ } else if (Array.isArray(radius)) {
+ r = {
+ topLeft: radius[0] ?? 0,
+ topRight: radius[1] ?? radius[0] ?? 0,
+ bottomRight: radius[2] ?? radius[0] ?? 0,
+ bottomLeft: radius[3] ?? radius[1] ?? radius[0] ?? 0,
+ }
+ } else {
+ r = {
+ topLeft: radius.topLeft ?? 0,
+ topRight: radius.topRight ?? 0,
+ bottomRight: radius.bottomRight ?? 0,
+ bottomLeft: radius.bottomLeft ?? 0,
+ }
+ }
+
+ const tl = Math.min(r.topLeft, width / 2, height / 2)
+ const tr = Math.min(r.topRight, width / 2, height / 2)
+ const br = Math.min(r.bottomRight, width / 2, height / 2)
+ const bl = Math.min(r.bottomLeft, width / 2, height / 2)
+
+ this.beginPath()
+ this.moveTo(x + tl, y)
+ this.lineTo(x + width - tr, y)
+ this.quadraticCurveTo(x + width, y, x + width, y + tr)
+ this.lineTo(x + width, y + height - br)
+ this.quadraticCurveTo(x + width, y + height, x + width - br, y + height)
+ this.lineTo(x + bl, y + height)
+ this.quadraticCurveTo(x, y + height, x, y + height - bl)
+ this.lineTo(x, y + tl)
+ this.quadraticCurveTo(x, y, x + tl, y)
+
+ return this
+ }
+ }
+
+ // -----------------------------
+ // resetTransform (older browsers)
+ // -----------------------------
+ if (typeof ctx.resetTransform !== 'function') {
+ ctx.resetTransform = function () {
+ this.setTransform(1, 0, 0, 1, 0, 0)
+ }
+ }
+
+ // -----------------------------
+ // filter (Android WebView)
+ // -----------------------------
+ if (!('filter' in ctx)) {
+ Object.defineProperty(ctx, 'filter', {
+ get: () => 'none',
+ set: () => {},
+ })
+ }
+
+ // -----------------------------
+ // imageSmoothingQuality
+ // -----------------------------
+ if (!('imageSmoothingQuality' in ctx)) {
+ Object.defineProperty(ctx, 'imageSmoothingQuality', {
+ get: () => 'low',
+ set: () => {},
+ })
+ }
+
+ return ctx
+}