Skip to content

Commit

Permalink
Merge branch 'collaboratiny' of github.com:univietw/moodle into colla…
Browse files Browse the repository at this point in the history
…boratiny
  • Loading branch information
univietw committed Sep 4, 2024
2 parents e82091d + 1e0e341 commit 5ea3d7b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 37 deletions.

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

Large diffs are not rendered by default.

60 changes: 52 additions & 8 deletions lib/editor/tiny/plugins/collaborative/amd/src/collaborater.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ let newHash = '';
//let newContent = '';
//let lastHash = '';
const INTERVALTIMEOUT = 1000;
const HEADER = "changes Index: a\n===================================================================\n";
const HEADER = "Index: a\n===================================================================\n";

const fetchOne = (methodname, args) => call([{
methodname,
args,
}])[0];
let intervalId = null;

async function sha1(message) {
const encoder = new TextEncoder();
Expand All @@ -54,7 +55,28 @@ async function sha1(message) {
}

// Example usage:

const insertCursorMarker = (editor) => {
const markerId = 'cursor-marker-' + new Date().getTime();
editor.selection.collapse();
editor.selection.setContent(`<span id="${markerId}"></span>`);
return markerId;
};
const restoreCursorPositionFromMarker = (editor, markerId) => {
const markerElement = editor.getBody().querySelector(`#${markerId}`);
if (markerElement) {
const range = editor.dom.createRng();
range.setStartAfter(markerElement);
range.collapse(true);
editor.selection.setRng(range);
editor.dom.remove(markerElement); // Clean up marker
// editor.focus();
} else {
// Fallback if marker not found
// editor.focus();
editor.selection.select(editor.getBody(), true);
editor.selection.collapse(false);
}
};


export const register = (editor) => {
Expand All @@ -72,7 +94,9 @@ export const register = (editor) => {
sha1(newContent).then(hash => {
newHash = hash;
if (currentHash === '') {
currentContent = newContent;
currentHash = newHash;
return;
}
if (newHash !== currentHash) {
let patch = jsDiff.createPatch('a', currentContent, newContent);
Expand All @@ -87,7 +111,7 @@ export const register = (editor) => {
return fetchOne('tiny_collaborate_save_changes', {
contextid: Options.getContextId(editor),
pagehash: Options.getPageHash(editor),
pageinstance: Options.getPageInstance(editor),
//pageinstance: Options.getPageInstance(editor),
elementid: editor.targetElm.id,
/*drftid: Options.getDraftItemId(editor),*/
oldcontenthash: currentHash,
Expand All @@ -105,26 +129,46 @@ export const register = (editor) => {
});
}
});
if (currentHash === '') {
return;
}
// const markerId = insertCursorMarker(editor);
let newContent2 = editor.getContent();
fetchOne('tiny_collaborate_get_changes', {
contextid: Options.getContextId(editor),
pagehash: Options.getPageHash(editor),
pageinstance: Options.getPageInstance(editor),
// pageinstance: Options.getPageInstance(editor),
elementid: editor.targetElm.id,
currenthash: currentHash,
}).then((result) => {
if (result) {
let changesMade = false;
for (let i in result) {
let change = result[i];
c.log('shorthcange', change);
let patch = HEADER + change;
c.log('changes', patch);
c.log('parsedPatch', jsDiff.parsePatch(patch));
newContent2 = jsDiff.applyPatch(newContent2, patch);
changesMade = true;
}
editor.setContent(newContent2);
sha1(newContent2).then(hash => {
currentHash = hash;
});
if (changesMade) {
if (newContent2 === false) {
c.log('Patch FAILED');
} else {
c.log('newContent2', newContent2);
editor.setContent(newContent2);
currentContent = newContent2;
sha1(newContent2).then(hash => {
currentHash = hash;
c.log('new hash', currentHash);
});
}
}
//clearInterval(intervalId);
}

// restoreCursorPositionFromMarker(editor, markerId);
});

}, INTERVALTIMEOUT);
Expand Down
23 changes: 12 additions & 11 deletions lib/editor/tiny/plugins/collaborative/classes/change_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ class change_manager {
/** @var int The contextid */
protected $contextid;

/** @var string The page hash reference */
protected $pagehash;

/** @var string The elementid for this editor */
protected $elementid;

Expand All @@ -49,30 +46,36 @@ class change_manager {
*/
public function __construct(
int $contextid,
string $pagehash,
string $elementid,
string $oldcontenthash
) {
global $DB;
$this->contextid = $contextid;
$this->pagehash = $pagehash;
$this->elementid = $elementid;
$this->oldcontenthash = $oldcontenthash;
}

public function add_collaborative_record($newcontenthash, $changes) {
global $DB,$USER;

if ($record = $DB->get_record('tiny_collaborative_changes', [
'newcontenthash' => $newcontenthash,
'elementid' => $this->elementid,
'contextid' => $this->contextid
])) {
return $record->id;
}

$record = new \stdClass();
$record->oldcontenthash = $this->oldcontenthash;
$record->newcontenthash = $newcontenthash;
$record->timemodified = time();
$record->changes = $changes;
$record->pagehash = $this->pagehash;
$record->contextid = $this->contextid;
$record->elementid = $this->elementid;
$record->oldcontenthash = $this->oldcontenthash;
$record->userid = $USER->id;


try {
$record->id = $DB->insert_record('tiny_collaborative_changes', $record);
} catch(\Exception $e) {
Expand All @@ -85,12 +88,10 @@ public function get_changes() {
global $DB;
$changesarray = [];
$currenthash = $this->oldcontenthash;
while ($change = $DB->get_record('tiny_collaborative_changes', ['oldcontenthash' => $currenthash,
'pagehash' => $this->pagehash,
while ($change = $DB->get_record('tiny_collaborative_changes', ['oldcontenthash' => $currenthash,
'elementid' => $this->elementid,
'contextid' => $this->contextid
]
)) {
])) {
$changesarray[] = $change->changes;
$currenthash = $change->newcontenthash;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class get_changes extends external_api {
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'contextid' => new external_value(PARAM_INT, 'The context id that owns the editor', VALUE_REQUIRED),
'pagehash' => new external_value(PARAM_ALPHANUMEXT, 'The page hash', VALUE_REQUIRED),
'elementid' => new external_value(PARAM_RAW, 'The ID of the element', VALUE_REQUIRED),
'currenthash' => new external_value(PARAM_ALPHANUMEXT, 'The ID of the element', VALUE_REQUIRED),
]);
Expand All @@ -53,37 +52,33 @@ public static function execute_parameters(): external_function_parameters {
* silently return and this is not treated as an error condition.
*
* @param int $contextid The context id of the owner
* @param string $pagehash The hash of the page
* @param string $pageinstance The instance id of the page
* @param string $elementid The id of the element
* @param int $draftid The id of the draftid to resume to
* @return null
*/
public static function execute(
int $contextid,
string $pagehash,
string $elementid,
string $elementid,
string $currenthash
): array {

[
'contextid' => $contextid,
'pagehash' => $pagehash,
'elementid' => $elementid,
'currenthash' => $currenthash,
] = self::validate_parameters(self::execute_parameters(), [
'contextid' => $contextid,
'pagehash' => $pagehash,
'elementid' => $elementid,
'currenthash' => $currenthash,
]);


// May have been called by a non-logged in user.
if (isloggedin() && !isguestuser()) {
$manager = new \tiny_collaborative\change_manager($contextid, $pagehash, $pageinstance, $elementid, $currenthash);
$manager = new \tiny_collaborative\change_manager($contextid, $elementid, $currenthash);
$changes = $manager->get_changes();
$positionmanager = new tiny_collaborative\position_manager($contextid,$pagehash,$elementid);
$positionmanager = new tiny_collaborative\position_manager($contextid, $elementid);
$positions = $positionmanager->get_user_positions();
}
return ['changes' => $changes, 'positions' => $positions];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class save_changes extends external_api {
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'contextid' => new external_value(PARAM_INT, 'The context id that owns the editor', VALUE_REQUIRED),
'pagehash' => new external_value(PARAM_ALPHANUMEXT, 'The page hash', VALUE_REQUIRED),
'elementid' => new external_value(PARAM_RAW, 'The ID of the element', VALUE_REQUIRED),
'oldcontenthash' => new external_value(PARAM_ALPHANUMEXT, 'The hash of the old status', VALUE_REQUIRED),
'newcontenthash' => new external_value(PARAM_ALPHANUMEXT, 'The hash of the new status', VALUE_REQUIRED),
Expand All @@ -49,15 +48,13 @@ public static function execute_parameters(): external_function_parameters {
/**
*
* @param int $contextid The context id of the owner
* @param string $pagehash The hash of the page
* @param string $pageinstance The instance id of the page
* @param string $elementid The id of the element
* @param string $drafttext The text to store
* @return null
*/
public static function execute(
int $contextid,
string $pagehash,
string $elementid,
string $oldcontenthash,
string $newcontenthash,
Expand All @@ -66,22 +63,20 @@ public static function execute(

[
'contextid' => $contextid,
'pagehash' => $pagehash,
'elementid' => $elementid,
'oldcontenthash' => $oldcontenthash,
'newcontenthash' => $newcontenthash,
'changes' => $changes
] = self::validate_parameters(self::execute_parameters(), [
'contextid' => $contextid,
'pagehash' => $pagehash,
'elementid' => $elementid,
'oldcontenthash' => $oldcontenthash,
'newcontenthash' => $newcontenthash,
'changes' => $changes
]);
// May have been called by a non-logged in user.
if (isloggedin() && !isguestuser()) {
$manager = new \tiny_collaborative\change_manager($contextid, $pagehash, $elementid, $oldcontenthash);
$manager = new \tiny_collaborative\change_manager($contextid, $elementid, $oldcontenthash);
$manager->add_collaborative_record($newcontenthash, $changes);
}

Expand Down
9 changes: 7 additions & 2 deletions lib/editor/tiny/plugins/collaborative/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<FIELD NAME="newcontenthash" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="20" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="changes" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="pagehash" TYPE="char" LENGTH="64" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="elementid" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
Expand All @@ -23,9 +22,15 @@
<INDEX NAME="colabsearch" UNIQUE="false" FIELDS="pagehash, elementid, contextid"/>
</INDEXES>
</TABLE>
<TABLE NAME="changeme" COMMENT="Default comment for the table, please edit me">
<TABLE NAME="tiny_collaborative_positions" COMMENT="The positions of the users in the document">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="elementid" TYPE="char" LENGTH="64" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="pageinstance" TYPE="char" LENGTH="64" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="position" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down

0 comments on commit 5ea3d7b

Please sign in to comment.