Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/
2 changes: 1 addition & 1 deletion js/editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/settings.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;
});
}
}
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"@nextcloud/auth": "^2.4.0",
"@nextcloud/axios": "^2.5.1",
"@nextcloud/browserslist-config": "^2.2.0",
"@nextcloud/dialogs": "^6.1.1",
"@nextcloud/event-bus": "^3.3.2",
"@nextcloud/files": "^3.10.2",
Expand Down
17 changes: 13 additions & 4 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,19 @@ import '@nextcloud/dialogs/style.css';

if (filePath)
{
url = generateUrl('/apps/files/?dir={currentDirectory}', {
currentDirectory: filePath.substring(0, filePath.lastIndexOf('/')),
fileId: data.id
});
const matches = [...filePath.matchAll(/\/\.attachments\.(\d+)\//g)];
if (matches.length) {
const match = matches[matches.length - 1];
url = generateUrl('/apps/files/files/{fileId}?dir={currentDirectory}&openfile=true', {
currentDirectory: filePath.substring(0, filePath.lastIndexOf(match[0])),
fileId: match[1],
});
} else {
url = generateUrl('/apps/files/?dir={currentDirectory}', {
currentDirectory: filePath.substring(0, filePath.lastIndexOf('/')),
fileId: data.id
});
}
}
else // ShareToken case
{
Expand Down
85 changes: 49 additions & 36 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ 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'

import PreviewComponent from './viewer.js'

// Some code is inspired by Mind Map app (https://github.com/ACTom/files_mindmap)

OCA.DrawIO = {
Expand Down Expand Up @@ -121,51 +121,64 @@ 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);
}
}
}
};

if (typeof OCA.Viewer !== 'undefined') {
OCA.Viewer.registerHandler({
id: 'drawio',
mimes: Object.values(OCA.DrawIO.Mimes).map(x => x.mime),
component: PreviewComponent,
group: null,
theme: 'default',
canCompare: true,
});
}

$(function ()
{
OCA.DrawIO.init();
Expand Down
78 changes: 78 additions & 0 deletions src/viewer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const generateRandom = () =>
Math.random()
.toString(36)
.replace(/[^a-z]+/g, '')
.substr(0, 10);

export default {
name: 'drawio-preview',
props: {
filename: { type: String, default: null },
fileid: { type: Number, default: null },
isEmbedded: { type: Boolean, default: false },
},
render(createElement) {
this.$emit('update:loaded', true);
const rnd = generateRandom();
const imgUrl = `/index.php/core/preview?fileId=${this.fileid}&x=1000&y=1000&a=1&${rnd}`;
const padding = 15;

this.$nextTick(() => {
const el = document.getElementById(
`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.
);
el.addEventListener('click', () => {
this.openEditor();
});
});
const img = new Image();
img.onload = function() {
const el = document.getElementById(
`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.
);
const img = document.createElement('div');
img.style.height = `${this.height}px`;
img.style.width = `100%`;
img.style.background = `url(${imgUrl}) no-repeat center/contain`;
img.style.cursor = 'pointer',
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.

Syntax error: using comma instead of semicolon. This line should end with a semicolon to be consistent with other property assignments and to avoid potential issues.

Suggested change
img.style.cursor = 'pointer',
img.style.cursor = 'pointer';

Copilot uses AI. Check for mistakes.
img.style.margin = `${padding}px`,

el.appendChild(img);
const h = this.height + padding * 2;
el.style.height = `${h}px`;
el.style.minHeight = `${h}px`;
}
img.src = imgUrl;
return createElement(
'div',
{
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',
'drawio-viewer__embedding',
],
},
'',
);
},
computed: {
isWB() {
var extension = this.filename.substr(this.filename.lastIndexOf('.') + 1).toLowerCase();
return String(extension == 'dwb');
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.

Using loose equality (==) and unnecessary String() conversion. The comparison should use strict equality (===) and String() wrapper is redundant since the result will already be a boolean.

Suggested change
return String(extension == 'dwb');
return extension === 'dwb';

Copilot uses AI. Check for mistakes.
},
},
mounted() {
if (!this.isEmbedded) {
this.openEditor();
}
},
methods: {
openEditor() {
OCA.DrawIO.OpenEditor(this.fileid, this.isWB);
},
},
};