OnShape helper

Various tweaks for OnShape, such as remap F2 for rename (SHIFT + N)

目前為 2025-06-03 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         OnShape helper
// @namespace    V@no
// @version      25.6.3-000924
// @description  Various tweaks for OnShape, such as remap F2 for rename (SHIFT + N)
// @author       V@no
// @license      MIT
// @match        https://cad.onshape.com/documents/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=onshape.com
// @grant        none
// ==/UserScript==

{
	"use strict";
/*
^ = CTRL
! = ALT
+ = SHIFT
*/
	const map = {
		"F2": {key: "N", code: "KeyN", keyCode: 78, shiftKey: true}
	};

	let mouseEvent = {};
	document.addEventListener("mousemove", evt =>
	{
		mouseEvent = evt;
	}, false);

	document.body.addEventListener("keydown", evt =>
	{
		let modifier = "";
		modifier = evt.altKey ? "!" : "";
		modifier = evt.shiftKey ? "+" : "";
		modifier = evt.ctrlKey || evt.metaKey ? "^" : "";
		const key = modifier + evt.code;
		if (!evt.isTrusted || !(key in map) || evt.altKey || evt.shiftKey || evt.ctrlKey || evt.metaKey)
			return console.log(evt, mouseEvent);

		if (mouseEvent.target)
		{
			evt.target.dispatchEvent(new KeyboardEvent(evt.type, Object.assign({}, evt, {key: " ", code: "space", keyCode: 32}, {bubbles: true})));
			mouseEvent.target.dispatchEvent(new PointerEvent("click", mouseEvent));
		}

		evt.target.dispatchEvent(new KeyboardEvent(evt.type, Object.assign({}, evt, map[key], {bubbles: true})));
	}, true);
	const css = `
.dimension-edit-container .ns-feature-parameter .bti-numeric-text,
.dimension-edit-container os-quantity-parameter input,
.dimension-edit
{
	max-width: unset;
}

div.OSH::before,
div.OSH::after {
  box-sizing: border-box;
}

div.OSH {
  display: inline-grid;
  vertical-align: top;
  align-items: center;
  position: relative;
}

div.OSH::after,
div.OSH input
{
  width: auto;
  min-width: 1em;
  grid-area: 1/2;
  font: inherit;
  padding: 0 0.25em 0 0;
  margin: 0;
  resize: none;
  background: none;
  -webkit-appearance: none;
     -moz-appearance: none;
          appearance: none;
  border: none;
}

div.OSH::after {
  content: attr(data-value) " ";
  visibility: hidden;
  white-space: pre-wrap;
}

div.OSH_conf_row > .OSH_conf {
	font-size: x-large;
	padding: 0 0.2em;
	line-height: 1em;
}

div.OSH_conf_row > .OSH_conf:hover {
	background-color: var(--os-table-cell-fill--hover);
}

div.OSH_conf_row > .OSH_conf.UP {
  order: 1;
}

div.OSH_conf_row > .OSH_conf.DOWN {
  order: 2;
}

div.OSH_conf_row > :not(.OSH_conf) {
  order: 3;
}
`;
	const elStyle = document.createElement("style");
	elStyle.id = "onShapeHelper";
	elStyle.textContent = css;
	document.head.append(elStyle);

	const dataValue = (el, value) =>
	{
		el.dataset.value = value;
	};
	const observer = new MutationObserver((mutationList, _observer) =>
	{
		for (const mutation of mutationList)
		{
			for(const node of mutation.addedNodes)
			{
				if (node.nodeType !== 1)
					continue;
				if (node.matches("input:not(.OSH)"))
				{
					node.classList.add("OSH");
					node.parentElement.classList.add("OSH");
					dataValue(node.parentElement, node.value);//node.dataset.bsOriginalTitle);
					node.addEventListener("input", evt => dataValue(evt.target.parentElement, evt.target.value));
				}
				if (!node.classList.contains("OSH_conf"))
				{
					const nlNodes = node.querySelectorAll(`a:not(.OSH_conf)[ng-click="configurationTable.moveParameterUp()"], a:not(.OSH_conf)[ng-click="configurationTable.moveParameterDown()"`);
					if (nlNodes.length > 0)
					{
						node.classList.add("OSH_conf");
						for(let i = 0; i < nlNodes.length; i++)
						{
							const elA = nlNodes[i];
							elA.classList.add("OSH_conf");
							const elParent = elA.closest("div.os-table-header-responsive-last-row>div.d-flex");
							elParent.prepend(elA);
							elParent.classList.add("OSH_conf_row");
							const type = elA.matches(`[ng-click="configurationTable.moveParameterUp()"]`);
							elA.classList.add(type ? "UP" : "DOWN");
							elA.title = elA.textContent;
							elA.textContent = type ? "▲" : "▼";
						}
					}
				}
			}
		}
	});
	observer.observe(document.body, {
		childList: true,
		subtree: true,
	});
}