Skip to content

方糖常见 (?) 问题

1. UI 类问题

1.1 引入外部字体时,不恰当字体大小导致元素宽度溢出

WARNING

具体原因存疑

import导入字体并设置字体大小时,使用手机浏览页面发现input宽度溢出。

css
input[type="text"] {
	min-width: 10vw !important;
  	max-width: 90vw !important;
    box-sizing: border-box;
}

即使用 !important 做上述修改,也仅在刷新的一瞬间正常。

1.1.1 解决方案

删除 font-size,或修改 font-size 大小。

或者尝试本地化载入(存疑,未证实)。

1.2 默认收起/隐藏侧边栏

1.2.1 解决方案

在全局JavaScript添加以下内容:

javascript
$(document).ready(function() {
    UIBar.stow();
    // 隐藏替换为UIBar.hide()
});

IMPORTANT

官方文档

关于UIBar.hide()

This does not reclaim the space reserved for the UI bar. Thus, a call to UIBar.stow() may also be necessary. Alternatively, if you simply want the UI bar gone completely and permanently, either using UIBar.destroy() or the StoryInterface special passage may be a better choice.

这不会回收为 UI 条保留的空间。因此,可能还需要调用 UIBar.stow()。或者,如果您只是想完全且永久地删除 UI 条,使用 UIBar.destroy() 或 StoryInterface 特殊通道 可能是更好的选择。

2. 数据类问题

2.1 存档更新时,如何解决旧存档不存在新变量

解决方案:

  1. Reddit
  2. SugarCube 文档

简而言之,在全局JS设置存档版本号,并在存档时对比版本号并修改存档数据。

当然不想这么做可以在导入update-var.js后,在全局JS里添加下面的代码。需要注意的是,简易库存中使用itemconsumable宏添加Item,得改为在全局JS里添加,具体请参考Item Api

2.1.1 Update Vars 使用示例

请确保全局JS里的 bindLoadMethod 有且仅有一个,多个 bindLoadMethod 会导致不必要的开销。

  • 示例一: 补全缺少变量,并且 store 变量始终为最新的。

    JavaScript
    bindLoadMethod({
    	replacements: ['store']		// 新替旧
    });
  • 示例二: 补全缺少变量,合并 user 并排除 user 下的 opts 合并。

JavaScript
bindLoadMethod({
	merge_paths: ['user'], 			// 深度合并路径
	exclude_paths: ['user.opts'] 	// 深度合并的排除路径
});

2.2 存档导出为字符串,并通过字符串导入

解决方案:

  1. SugarCube 文档

SugarCube 已经实现将存档文件导出为 Base64 字符串,以及将 Base64 存档字符串导入。其在文档中的用法如下:

JavaScript
// 导出
try {
	const base64Save = Save.base64.export();
	/* Do something with the saves bundle. */
}
catch (error) {
	/* Failure.  Handle the error. */
	console.error(error);
	UI.alert(error);
}
// 导入
Save.base64.import(base64Bundle)
	.then(() => {
		/* Success.  Do something special. */
	})
	.catch(error => {
		/* Failure.  Handle the error. */
		console.error(error);
		UI.alert(error);
	});

如果依旧没有头绪,请参考下边简单示例:

SugarCube
<!-- 片段名: StoryMenu -->
<<link '导出存档到剪切板'>>
<<script>>
try {
	const base64Save = Save.base64.export();
	navigator.clipboard.writeText(base64Save).then(function() {  
            UI.alert('复制成功');
        }).catch(function(err) {
           	UI.alert('复制失败');
        });
}
catch (error) {
	UI.alert(error);
}
<</script>>
<</link>>

<<link '导入存档'>>
<<run Dialog
	.create("导入存档")
	.wikiPassage("导入存档")
	.open();>>
<</link>>

<!-- 片段名: 导入存档 -->
<<set _base64Bundle = ''>>
<<textarea "_base64Bundle" "" autofocus>>
<<button '导入存档'>>
<<script>>
Save.base64.import(State.temporary.base64Bundle.trim())
	.then(() => {
		UI.alert('存档导入成功');
	})
	.catch(error => {
		UI.alert(error);
	});
<</script>>
<</button>>