Tldraw Chinese Font Support

为Tldraw添加中文字体支持,自动监听字体变化

目前為 2024-12-19 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Tldraw Chinese Font Support
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  为Tldraw添加中文字体支持,自动监听字体变化
// @author       Your Name
// @match        https://www.tldraw.com/*
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 添加字体样式
    // 四种字体:draw : 手写字体
    // sans : 无衬线字体,与中文的黑体对应
    // serif : 衬线字体,与中文的宋体对应
    // mono : 等宽字体,与中文的等宽字体对应
    const fontStyles = `
        [data-font="draw"] {
            font-family: "字语青梅硬笔", "LXGW WenKai", cursive !important;
        }
        [data-font="sans"] {
            font-family: "仓耳玄三01简繁", "PingFang SC", sans-serif !important;
        }
        [data-font="serif"] {
            font-family: "FZYouSongS 509R", "宋体", "STSong", serif !important;
        }
        [data-font="mono"] {
            font-family: "等距更纱黑体 SC" "LXGW WenKai Mono", "Source Han Mono SC", monospace !important;
        }
    `;

    // 创建样式元素
    const styleElement = document.createElement('style');
    styleElement.textContent = fontStyles;
    document.head.appendChild(styleElement);

    // 创建 MutationObserver 来监听属性变化
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.type === 'attributes' && mutation.attributeName === 'data-font') {
                const element = mutation.target;
                const fontType = element.getAttribute('data-font');
                console.log('Font changed to:', fontType);
            }
        });
    });

    // 监听配置
    const observerConfig = {
        attributes: true,
        attributeFilter: ['data-font'],
        subtree: true
    };

    // 等待页面加载完成后开始监听
    window.addEventListener('load', () => {
        // 开始监听整个文档
        observer.observe(document.body, observerConfig);
        console.log('Chinese font support enabled');
    });
})();