| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  | import ws from "./ws.js"; | 
					
						
							| 
									
										
										
										
											2021-04-16 23:01:56 +02:00
										 |  |  | import froca from "./froca.js"; | 
					
						
							| 
									
										
										
										
											2025-01-13 23:18:10 +02:00
										 |  |  | import type FAttribute from "../entities/fattribute.js"; | 
					
						
							|  |  |  | import type FNote from "../entities/fnote.js"; | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-19 20:47:02 +02:00
										 |  |  | async function renderAttribute(attribute: FAttribute, renderIsInheritable: boolean) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     const isInheritable = renderIsInheritable && attribute.isInheritable ? `(inheritable)` : ""; | 
					
						
							| 
									
										
										
										
											2020-09-08 21:45:07 +02:00
										 |  |  |     const $attr = $("<span>"); | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     if (attribute.type === "label") { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |         $attr.append(document.createTextNode(`#${attribute.name}${isInheritable}`)); | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (attribute.value) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             $attr.append("="); | 
					
						
							| 
									
										
										
										
											2020-09-08 21:45:07 +02:00
										 |  |  |             $attr.append(document.createTextNode(formatValue(attribute.value))); | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     } else if (attribute.type === "relation") { | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  |         if (attribute.isAutoLink) { | 
					
						
							| 
									
										
										
										
											2020-09-08 21:45:07 +02:00
										 |  |  |             return $attr; | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-05 23:41:11 +02:00
										 |  |  |         // when the relation has just been created, then it might not have a value
 | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  |         if (attribute.value) { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |             $attr.append(document.createTextNode(`~${attribute.name}${isInheritable}=`)); | 
					
						
							| 
									
										
										
										
											2024-12-19 19:23:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             const link = await createLink(attribute.value); | 
					
						
							|  |  |  |             if (link) { | 
					
						
							|  |  |  |                 $attr.append(link); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |         ws.logError(`Unknown attr type: ${attribute.type}`); | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-09-08 21:45:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return $attr; | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-19 20:47:02 +02:00
										 |  |  | function formatValue(val: string) { | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  |     if (/^[\p{L}\p{N}\-_,.]+$/u.test(val)) { | 
					
						
							|  |  |  |         return val; | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     } else if (!val.includes('"')) { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |         return `"${val}"`; | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     } else if (!val.includes("'")) { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |         return `'${val}'`; | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     } else if (!val.includes("`")) { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |         return `\`${val}\``; | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |         return `"${val.replace(/"/g, '\\"')}"`; | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-19 20:47:02 +02:00
										 |  |  | async function createLink(noteId: string) { | 
					
						
							| 
									
										
										
										
											2021-04-16 22:57:37 +02:00
										 |  |  |     const note = await froca.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2020-09-08 21:45:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!note) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return $("<a>", { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |         href: `#root/${noteId}`, | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         class: "reference-link" | 
					
						
							|  |  |  |     }).text(note.title); | 
					
						
							| 
									
										
										
										
											2020-09-08 21:45:07 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-08-27 14:54:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-19 20:47:02 +02:00
										 |  |  | async function renderAttributes(attributes: FAttribute[], renderIsInheritable: boolean) { | 
					
						
							| 
									
										
										
										
											2020-12-03 22:54:24 +01:00
										 |  |  |     const $container = $('<span class="rendered-note-attributes">'); | 
					
						
							| 
									
										
										
										
											2020-09-08 21:45:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (let i = 0; i < attributes.length; i++) { | 
					
						
							|  |  |  |         const attribute = attributes[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const $attr = await renderAttribute(attribute, renderIsInheritable); | 
					
						
							|  |  |  |         $container.append($attr.html()); // .html() to get only inner HTML, we don't want any spans
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (i < attributes.length - 1) { | 
					
						
							|  |  |  |             $container.append(" "); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-27 14:54:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-08 21:45:07 +02:00
										 |  |  |     return $container; | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-30 22:58:42 +03:00
										 |  |  | const HIDDEN_ATTRIBUTES = [ | 
					
						
							|  |  |  |     "originalFileName", | 
					
						
							|  |  |  |     "fileSize", | 
					
						
							|  |  |  |     "template", | 
					
						
							|  |  |  |     "inherit", | 
					
						
							|  |  |  |     "cssClass", | 
					
						
							|  |  |  |     "iconClass", | 
					
						
							|  |  |  |     "pageSize", | 
					
						
							|  |  |  |     "viewType", | 
					
						
							|  |  |  |     "geolocation", | 
					
						
							|  |  |  |     "docName", | 
					
						
							|  |  |  |     "webViewSrc" | 
					
						
							|  |  |  | ]; | 
					
						
							| 
									
										
										
										
											2021-03-12 20:39:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-19 20:47:02 +02:00
										 |  |  | async function renderNormalAttributes(note: FNote) { | 
					
						
							| 
									
										
										
										
											2021-01-23 21:41:02 +01:00
										 |  |  |     const promotedDefinitionAttributes = note.getPromotedDefinitionAttributes(); | 
					
						
							|  |  |  |     let attrs = note.getAttributes(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (promotedDefinitionAttributes.length > 0) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         attrs = attrs.filter((attr) => !!promotedDefinitionAttributes.find((promAttr) => promAttr.isDefinitionFor(attr))); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         attrs = attrs.filter((attr) => !attr.isDefinition() && !attr.isAutoLink && !HIDDEN_ATTRIBUTES.includes(attr.name) && attr.noteId === note.noteId); | 
					
						
							| 
									
										
										
										
											2021-01-23 21:41:02 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-03 22:54:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const $renderedAttributes = await renderAttributes(attrs, false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         count: attrs.length, | 
					
						
							|  |  |  |         $renderedAttributes | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-12-03 22:54:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-17 00:08:28 +02:00
										 |  |  | export default { | 
					
						
							| 
									
										
										
										
											2020-09-08 21:45:07 +02:00
										 |  |  |     renderAttribute, | 
					
						
							| 
									
										
										
										
											2020-12-03 22:54:24 +01:00
										 |  |  |     renderAttributes, | 
					
						
							|  |  |  |     renderNormalAttributes | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  | }; |