Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: register mime types via template manager
Signed-off-by: Peter Birrer <[email protected]>
  • Loading branch information
pbirrer committed Aug 22, 2025
commit c0b744363812eaa1412e428835d96ae8828dbb84
2 changes: 1 addition & 1 deletion js/main.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use OCA\Drawio\Controller\SettingsController;
use OCA\Drawio\Preview\DrawioPreview;
use OCA\Drawio\Listeners\FileDeleteListener;
use OCA\Drawio\Listeners\RegisterTemplateCreatorListener;
use OCP\Files\Template\RegisterTemplateCreatorEvent;

use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\Node\NodeDeletedEvent;
Expand All @@ -32,11 +34,13 @@

class Application extends App {

public const APP_ID = "drawio";

public $appConfig;

public function __construct(array $urlParams = [])
{
$appName = "drawio";
$appName = self::APP_ID;

parent::__construct($appName, $urlParams);

Expand Down Expand Up @@ -139,5 +143,6 @@ public function __construct(array $urlParams = [])
/** @var IEventDispatcher $eventDispatcher */
$newEventDispatcher = $server->query(IEventDispatcher::class);
$newEventDispatcher->addServiceListener(NodeDeletedEvent::class, FileDeleteListener::class);
$newEventDispatcher->addServiceListener(RegisterTemplateCreatorEvent::class, RegisterTemplateCreatorListener::class);
}
}
37 changes: 37 additions & 0 deletions lib/Listeners/RegisterTemplateCreatorListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace OCA\Drawio\Listeners;

use OCA\Drawio\AppInfo\Application;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Template\RegisterTemplateCreatorEvent;
use OCP\Files\Template\TemplateFileCreator;
use OCP\IL10N;

final class RegisterTemplateCreatorListener implements IEventListener {
public function __construct(
private IL10N $l10n,
) {
}

public function handle(Event $event): void {
if (!($event instanceof RegisterTemplateCreatorEvent)) {
return;
}
$event->getTemplateManager()->registerTemplateFileCreator(function () {
$drawio = new TemplateFileCreator(Application::APP_ID, $this->l10n->t('New draw.io Diagram'), '.drawio');
$drawio->addMimetype('application/x-drawio');
$drawio->setIconSvgInline(file_get_contents(__DIR__ . '/../../img/drawio.svg'));
$drawio->setActionLabel($this->l10n->t('New draw.io Diagram'));
return $drawio;
});
$event->getTemplateManager()->registerTemplateFileCreator(function () {
$dwb = new TemplateFileCreator(Application::APP_ID, $this->l10n->t('New draw.io Whiteboard'), '.dwb');
$dwb->addMimetype('application/x-drawio-wb');
$dwb->setIconSvgInline(file_get_contents(__DIR__ . '/../../img/dwb.svg'));
$dwb->setActionLabel($this->l10n->t('New draw.io Whiteboard'));
return $dwb;
});
}
}
72 changes: 36 additions & 36 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ import axios from '@nextcloud/axios'
import {
DefaultType,
FileAction,
addNewFileMenuEntry,
registerFileAction,
File,
Permission,
getNavigation,
} from '@nextcloud/files'
import { getCurrentUser } from '@nextcloud/auth'
import { emit } from '@nextcloud/event-bus'
Expand Down Expand Up @@ -123,48 +121,50 @@ OCA.DrawIO = {
};
},

registerNewFileMenuPlugin: () => {
function getUniqueName(name, ext, names)
init: async function ()
{
if ($('#isPublic').val() === '1' && !$('#filestable').length)
{
let newName;
var fileName = $('#filename').val();
var mimeType = $('#mimetype').val();
var sharingToken = $('#sharingToken').val();
var extension = fileName.substr(fileName.lastIndexOf('.') + 1).toLowerCase();
var isWB = String(extension == 'dwb');

do
if (!OCA.DrawIO.Mimes[extension] || OCA.DrawIO.Mimes[extension].mime != mimeType)
{
newName = name + '-' + Math.round(Math.random() * 1000000) + '.' + ext;
return;
}
while (names.includes(newName))

return newName;
}

function addMenuEntry(ext, attr)
{
addNewFileMenuEntry({
id: 'drawIoDiagram_' + ext,
displayName: attr.newStr,
enabled(node) {
return (node.permissions & OC.PERMISSION_CREATE) !== 0;
},
iconClass: attr.css,
async handler(context, content)
{
const contentNames = content.map((node) => node.basename);
const fileName = getUniqueName(attr.newStr, ext, contentNames);
OCA.DrawIO.CreateNewFile(fileName, context, ext, attr.mime);
}
var button = document.createElement('a');
button.href = generateUrl('apps/' + OCA.DrawIO.AppName + '/edit?shareToken={shareToken}&isWB={isWB}&lightbox=true', {
shareToken: sharingToken,
isWB: isWB
});
}
button.className = 'button';
button.innerText = t(OCA.DrawIO.AppName, 'Open in Draw.io');
$('#preview').append(button);

for (const ext in OCA.DrawIO.Mimes)
{
addMenuEntry(ext, OCA.DrawIO.Mimes[ext]);
}
},
// If the file is editable, add a button to edit it
var url = generateUrl('/apps/' + OCA.DrawIO.AppName + '/ajax/getFileInfo?shareToken={shareToken}',
{
shareToken: sharingToken
});

var response = await axios.get(url);

init: async function ()
{
OCA.DrawIO.registerNewFileMenuPlugin();
OCA.DrawIO.registerFileActions();
if (response.status == 200 && response.data.writeable)
{
var editButton = document.createElement('a');
editButton.href = generateUrl('apps/' + OCA.DrawIO.AppName + '/edit?shareToken={shareToken}&isWB={isWB}', {
shareToken: sharingToken,
isWB: isWB
});
editButton.className = 'button';
editButton.innerText = t(OCA.DrawIO.AppName, 'Edit in Draw.io');
$('#preview').append(editButton);
}
}
}
};

Expand Down
1 change: 1 addition & 0 deletions src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default {
attrs: { id: `drawoi-${rnd}` },
Copy link

Copilot AI Oct 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected spelling of 'drawoi' to 'drawio'.

Copilot uses AI. Check for mistakes.
style: {
background: 'white',
cursor: 'pointer',
},
class: [
'drawio',
Expand Down