본문으로 이동

미디어위키:Gadget-MetadataEditor.js

리버티게임(개발), 모두가 만들어가는 자유로운 게임
Senouis (토론 | 기여)님의 2025년 1월 31일 (금) 20:37 판 (커스텀 버전으로 교체)

참고: 설정을 저장한 후에 바뀐 점을 확인하기 위해서는 브라우저의 캐시를 새로 고쳐야 합니다.

  • 파이어폭스 / 사파리: Shift 키를 누르면서 새로 고침을 클릭하거나, Ctrl-F5 또는 Ctrl-R을 입력 (Mac에서는 ⌘-R)
  • 구글 크롬: Ctrl-Shift-R키를 입력 (Mac에서는 ⌘-Shift-R)
  • 엣지: Ctrl 키를 누르면서 새로 고침을 클릭하거나, Ctrl-F5를 입력.
var metadata_schema;
var jsonEditorContainer;
var metadata_editorvalue;
let editor;

const saveToDocument = (ev) => {
	const jsonValue = editor.getValue();
	if (!jsonValue) {
		mw.notify("메타데이터를 저장할 수 없습니다.", {type: "error"});
		return;
	}
	else if (editor.validate().length) {
		console.log(editor.getValue());
		mw.notify("메타데이터가 적절한지 확인하는 데에 실패했습니다.", {type: "error"});
		return;
	}
	const api = new mw.Api();
	api.postWithEditToken( {
		"action": "edit", 
		"title": mw.config.get( "wgPageName" ) + "/game.json",
		"text": JSON.stringify(jsonValue),
		"summary": "메타데이터 수정",
		"contentmodel": "json"
	} ).then((result) =>{
		mw.notify("메타데이터를 저장했습니다!", {type: "success"});
	}).catch((error, result) => {
		mw.notify("메타데이터 저장 실패!" + error + " -> " + JSON.stringify(result), {type: "error"});
	});
};

const addMetadataTab = (resultPromise) => {
	resultPromise.then((result) => {
		metadata_schema = result;
	metadata_editorvalue = document.querySelector('#value');
	// JSONEditor.defaults.languages.ko = TRANSLATION;
	JSONEditor.defaults.language = "ko";
	// 탭 추가  
	let metadataTab = mw.util.addPortletLink("p-associated-pages", "#", "메타데이터 편집", "metadata-edittab", "메타데이터를 편집합니다");
	metadataTab.addEventListener('click', (ev) => {
		var targetJSON = `https://dev.libertygame.work/index.php/${mw.config.get("wgPageName")}/game.json?action=raw`;
		fetch(targetJSON).then(function(result){return result.json(); }).then(function (response) {
		  document.getElementById("jsoneditor-container").style.display = "block";
		  editor = new JSONEditor(jsonEditorContainer, {
		  	mode: 'tree',
		    schema: metadata_schema,
		    theme: 'tailwind',
		    iconlib: 'openiconic',
		    keep_oneof_values: false,
		    disable_edit_json: true,
		    disable_collapse : true,
		    array_controls_top : true,
		    startval:response
		  });
	    }).catch((e) => {console.log('JSON 에디터 로딩 후 JSON 타겟 가져오기 실패')});
	});	
	// 메타데이터 편집기 추가
	let metadataEditor = document.createElement("div");
	metadataEditor.id = "jsoneditor-container";
	metadataEditor.style.display = "none";
	metadataEditor.style['background-color'] = "#bbeebb";
	metadataEditor.style.position= "fixed";
	metadataEditor.style["z-index"] = 8;
	metadataEditor.style.top = "80px";
    document.getElementById("bodyContent").appendChild(metadataEditor);
    jsonEditorContainer = metadataEditor;
    let metadataCloseButton = document.createElement("button");
	metadataCloseButton.onclick = (ev) => {ev.target.parentElement.style.display = "none";};
	metadataCloseButton.innerText = "편집기 닫기";
	metadataEditor.appendChild(metadataCloseButton);
	let metadataSaveButton = document.createElement("button");
	metadataSaveButton.onclick = saveToDocument;
	metadataSaveButton.innerText = "저장하기";
	metadataEditor.appendChild(metadataSaveButton);
	});
};

const addJSONEditor = () => {
	const title = mw.config.get("wgPageName");
	const api = new mw.Api();
	Promise.all([
		api.getCategories(title),
		mw.loader.getScript("https://cdn.jsdelivr.net/gh/@Xen-alpha/json-editor@latest/dist/jsoneditor.js"),
		fetch("https://dev.libertygame.work/index.php/%EB%A6%AC%EB%B2%84%ED%8B%B0%EA%B2%8C%EC%9E%84:%EA%B2%8C%EC%9E%84_%EB%A9%94%ED%83%80%EB%8D%B0%EC%9D%B4%ED%84%B0/%EC%8A%A4%ED%82%A4%EB%A7%88.json?action=raw")
	]).then((values) =>{
		if (!values[0]) return;
		let esc = true;
		for (const category of values[0]){
			if (category.title === "리버티게임"){
				esc = false;
			}
		}
		if (esc) return;
		addMetadataTab(values[2].json());
	}).catch((e) => {console.log('메타데이터 편집기 로딩 실패')});
	
};

$(addJSONEditor);